From 8a1474e8d6ac821629538bba17cc891ca500ea1c Mon Sep 17 00:00:00 2001 From: Alex Christoffer Rasmussen Date: Fri, 5 Apr 2024 23:06:40 +0200 Subject: [PATCH] Improve error handling in server (#812) * add functions to convert all errors to `ErrorResponse` type * replace all `{ message: error.toString() }` with `toErrorResponse(error)` * refactor: all controllers to to use getErrorMessage and add types to the Response * 404 for nonexistent api routes --- .../panel/sources-panel/SourcesPanel.tsx | 4 +- .../custom-fields/customFields.controller.ts | 20 ++++---- apps/server/src/api-data/db/db.controller.ts | 34 ++++++++----- .../src/api-data/http/http.controller.ts | 4 +- apps/server/src/api-data/index.ts | 5 ++ .../server/src/api-data/osc/osc.controller.ts | 4 +- .../api-data/project/project.controller.ts | 4 +- .../api-data/rundown/rundown.controller.ts | 25 ++++++--- .../api-data/settings/settings.controller.ts | 5 +- .../src/api-data/sheets/sheets.controller.ts | 51 ++++++++++++++----- .../url-presets/urlPresets.controller.ts | 4 +- .../view-settings/viewSettings.controller.ts | 4 +- .../src/api-integration/integration.router.ts | 10 ++-- packages/utils/index.ts | 2 +- packages/utils/src/generic/generic.ts | 2 +- 15 files changed, 120 insertions(+), 58 deletions(-) diff --git a/apps/client/src/features/app-settings/panel/sources-panel/SourcesPanel.tsx b/apps/client/src/features/app-settings/panel/sources-panel/SourcesPanel.tsx index 7bb896df4..fb804052b 100644 --- a/apps/client/src/features/app-settings/panel/sources-panel/SourcesPanel.tsx +++ b/apps/client/src/features/app-settings/panel/sources-panel/SourcesPanel.tsx @@ -2,7 +2,7 @@ import { ChangeEvent, useRef, useState } from 'react'; import { Button, Input } from '@chakra-ui/react'; import { IoCloudOutline } from '@react-icons/all-files/io5/IoCloudOutline'; import { IoDownloadOutline } from '@react-icons/all-files/io5/IoDownloadOutline'; -import { ImportMap, unpackError } from 'ontime-utils'; +import { getErrorMessage, ImportMap } from 'ontime-utils'; import { getWorksheetNames as getWorksheetNamesExcel, @@ -59,7 +59,7 @@ export default function SourcesPanel() { setImportFlow('excel'); setHasFile('done'); } catch (error) { - const errorMessage = unpackError(error); + const errorMessage = getErrorMessage(error); setError(`Error uploading file: ${errorMessage}`); setWorksheets(null); setHasFile('none'); diff --git a/apps/server/src/api-data/custom-fields/customFields.controller.ts b/apps/server/src/api-data/custom-fields/customFields.controller.ts index 42c428c08..71c55f5f8 100644 --- a/apps/server/src/api-data/custom-fields/customFields.controller.ts +++ b/apps/server/src/api-data/custom-fields/customFields.controller.ts @@ -1,7 +1,8 @@ -import { CustomField, CustomFields } from 'ontime-types'; +import { CustomField, CustomFields, ErrorResponse } from 'ontime-types'; import type { Request, Response } from 'express'; +import { getErrorMessage } from 'ontime-utils'; import { createCustomField, editCustomField, @@ -14,36 +15,37 @@ export async function getCustomFields(_req: Request, res: Response res.json(customFields); } -// Expects { label: