Skip to content

Commit 2df6c23

Browse files
[OGUI-1494] Refactor: Reuse existing error handling from @alice02/web-ui (#2726)
* This pull request simplifies how errors are managed by using the error handling already built in @alice02/web-ui.
1 parent e2871da commit 2df6c23

File tree

10 files changed

+232
-186
lines changed

10 files changed

+232
-186
lines changed

QualityControl/lib/controllers/LayoutController.js

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
import assert from 'assert';
1818
import { LayoutDto } from './../dtos/LayoutDto.js';
1919
import { LayoutPatchDto } from './../dtos/LayoutPatchDto.js';
20+
2021
import {
21-
updateExpressResponseFromNativeError,
22-
} from './../errors/updateExpressResponseFromNativeError.js';
23-
import { InvalidInputError } from './../errors/InvalidInputError.js';
24-
import { UnauthorizedAccessError } from './../errors/UnauthorizedAccessError.js';
25-
import { NotFoundError } from './../errors/NotFoundError.js';
22+
InvalidInputError,
23+
NotFoundError,
24+
UnauthorizedAccessError,
25+
updateAndSendExpressResponseFromNativeError,
26+
}
27+
from '@aliceo2/web-ui';
2628

2729
/**
2830
* Gateway for all HTTP requests with regards to QCG Layouts
@@ -59,7 +61,7 @@ export class LayoutController {
5961
const layouts = await this._dataService.listLayouts(filter);
6062
res.status(200).json(layouts);
6163
} catch {
62-
updateExpressResponseFromNativeError(res, new Error('Unable to retrieve layouts'));
64+
updateAndSendExpressResponseFromNativeError(res, new Error('Unable to retrieve layouts'));
6365
}
6466
}
6567

@@ -74,13 +76,13 @@ export class LayoutController {
7476

7577
try {
7678
if (!id) {
77-
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing parameter "id" of layout'));
79+
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing parameter "id" of layout'));
7880
} else {
7981
const layout = await this._dataService.readLayout(id);
8082
res.status(200).json(layout);
8183
}
8284
} catch {
83-
updateExpressResponseFromNativeError(res, new Error(`Unable to retrieve layout with id: ${id}`));
85+
updateAndSendExpressResponseFromNativeError(res, new Error(`Unable to retrieve layout with id: ${id}`));
8486
}
8587
}
8688

@@ -102,14 +104,14 @@ export class LayoutController {
102104
} else if (runDefinition) {
103105
layoutName = runDefinition;
104106
} else {
105-
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing query parameters'));
107+
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing query parameters'));
106108
return;
107109
}
108110
try {
109111
const layout = await this._dataService.readLayoutByName(layoutName);
110112
res.status(200).json(layout);
111113
} catch (error) {
112-
updateExpressResponseFromNativeError(res, error);
114+
updateAndSendExpressResponseFromNativeError(res, error);
113115
}
114116
}
115117

@@ -125,15 +127,18 @@ export class LayoutController {
125127
const { id } = req.params;
126128
try {
127129
if (!id) {
128-
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing parameter "id" of layout'));
130+
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing parameter "id" of layout'));
129131
} else if (!req.body) {
130-
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing body content to update layout with'));
132+
updateAndSendExpressResponseFromNativeError(
133+
res,
134+
new InvalidInputError('Missing body content to update layout with'),
135+
);
131136
} else {
132137
const { personid } = req.session;
133138
const { owner_id } = await this._dataService.readLayout(id);
134139

135140
if (Number(owner_id) !== Number(personid)) {
136-
updateExpressResponseFromNativeError(
141+
updateAndSendExpressResponseFromNativeError(
137142
res,
138143
new UnauthorizedAccessError('Only the owner of the layout can update it'),
139144
);
@@ -142,7 +147,7 @@ export class LayoutController {
142147
try {
143148
layoutProposed = await LayoutDto.validateAsync(req.body);
144149
} catch (error) {
145-
updateExpressResponseFromNativeError(
150+
updateAndSendExpressResponseFromNativeError(
146151
res,
147152
new Error(`Failed to update layout ${error?.details?.[0]?.message || ''}`),
148153
);
@@ -152,7 +157,7 @@ export class LayoutController {
152157
const layouts = await this._dataService.listLayouts({ name: layoutProposed.name });
153158
const layoutExistsWithName = layouts.every((layout) => layout.id !== layoutProposed.id);
154159
if (layouts.length > 0 && layoutExistsWithName) {
155-
updateExpressResponseFromNativeError(
160+
updateAndSendExpressResponseFromNativeError(
156161
res,
157162
new InvalidInputError(`Proposed layout name: ${layoutProposed.name} already exists`),
158163
);
@@ -163,7 +168,7 @@ export class LayoutController {
163168
}
164169
}
165170
} catch (error) {
166-
updateExpressResponseFromNativeError(res, error);
171+
updateAndSendExpressResponseFromNativeError(res, error);
167172
}
168173
}
169174

@@ -177,12 +182,15 @@ export class LayoutController {
177182
const { id } = req.params;
178183
try {
179184
if (!id) {
180-
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing parameter "id" of layout to delete'));
185+
updateAndSendExpressResponseFromNativeError(
186+
res,
187+
new InvalidInputError('Missing parameter "id" of layout to delete'),
188+
);
181189
} else {
182190
const { personid, name } = req.session;
183191
const { owner_name, owner_id } = await this._dataService.readLayout(id);
184192
if (owner_name !== name || owner_id !== personid) {
185-
updateExpressResponseFromNativeError(
193+
updateAndSendExpressResponseFromNativeError(
186194
res,
187195
new UnauthorizedAccessError('Only the owner of the layout can delete it'),
188196
);
@@ -192,7 +200,7 @@ export class LayoutController {
192200
}
193201
}
194202
} catch {
195-
updateExpressResponseFromNativeError(res, new Error(`Unable to delete layout with id: ${id}`));
203+
updateAndSendExpressResponseFromNativeError(res, new Error(`Unable to delete layout with id: ${id}`));
196204
}
197205
}
198206

@@ -207,7 +215,7 @@ export class LayoutController {
207215
try {
208216
layoutProposed = await LayoutDto.validateAsync(req.body);
209217
} catch (error) {
210-
updateExpressResponseFromNativeError(
218+
updateAndSendExpressResponseFromNativeError(
211219
res,
212220
new InvalidInputError(`Failed to validate layout: ${error?.details[0]?.message || ''}`),
213221
);
@@ -216,7 +224,7 @@ export class LayoutController {
216224
try {
217225
const layouts = await this._dataService.listLayouts({ name: layoutProposed.name });
218226
if (layouts.length > 0) {
219-
updateExpressResponseFromNativeError(
227+
updateAndSendExpressResponseFromNativeError(
220228
res,
221229
new InvalidInputError(`Proposed layout name: ${layoutProposed.name} already exists`),
222230
);
@@ -225,7 +233,7 @@ export class LayoutController {
225233
const result = await this._dataService.createLayout(layoutProposed);
226234
res.status(201).json(result);
227235
} catch {
228-
updateExpressResponseFromNativeError(res, new Error('Unable to create new layout'));
236+
updateAndSendExpressResponseFromNativeError(res, new Error('Unable to create new layout'));
229237
}
230238
}
231239

@@ -238,27 +246,30 @@ export class LayoutController {
238246
async patchLayoutHandler(req, res) {
239247
const { id } = req.params;
240248
if (!id) {
241-
updateExpressResponseFromNativeError(res, new InvalidInputError('Missing ID'));
249+
updateAndSendExpressResponseFromNativeError(res, new InvalidInputError('Missing ID'));
242250
} else {
243251
let layout = {};
244252
try {
245253
layout = await LayoutPatchDto.validateAsync(req.body);
246254
} catch {
247-
updateExpressResponseFromNativeError(res, new InvalidInputError('Invalid request body to update layout'));
255+
updateAndSendExpressResponseFromNativeError(
256+
res,
257+
new InvalidInputError('Invalid request body to update layout'),
258+
);
248259
return;
249260
}
250261

251262
try {
252263
await this._dataService.readLayout(id);
253264
} catch {
254-
updateExpressResponseFromNativeError(res, new NotFoundError(`Unable to find layout with id: ${id}`));
265+
updateAndSendExpressResponseFromNativeError(res, new NotFoundError(`Unable to find layout with id: ${id}`));
255266
return;
256267
}
257268
try {
258269
const layoutUpdated = await this._dataService.updateLayout(id, layout);
259270
res.status(201).json(layoutUpdated);
260271
} catch {
261-
updateExpressResponseFromNativeError(res, new Error(`Unable to update layout with id: ${id}`));
272+
updateAndSendExpressResponseFromNativeError(res, new Error(`Unable to update layout with id: ${id}`));
262273
return;
263274
}
264275
}

QualityControl/lib/controllers/StatusController.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* or submit itself to any jurisdiction.
1313
*/
1414

15+
import { ServiceUnavailableError, updateAndSendExpressResponseFromNativeError } from '@aliceo2/web-ui';
16+
1517
/**
1618
* Gateway for all calls with regards to the status of the framework and its dependencies
1719
*/
@@ -48,7 +50,10 @@ export class StatusController {
4850
const info = await this._statusService.retrieveFrameworkInfo();
4951
res.status(200).json(info);
5052
} catch (error) {
51-
res.status(503).json({ message: error.message || error });
53+
updateAndSendExpressResponseFromNativeError(
54+
res,
55+
new ServiceUnavailableError(error.message || error),
56+
);
5257
}
5358
}
5459
}

QualityControl/lib/errors/InvalidInputError.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

QualityControl/lib/errors/NotFoundError.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

QualityControl/lib/errors/UnauthorizedAccessError.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

QualityControl/lib/errors/updateExpressResponseFromNativeError.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

QualityControl/lib/middleware/minimumRole.middleware.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212
* or submit itself to any jurisdiction.
1313
*/
1414

15+
import { UnauthorizedAccessError, updateAndSendExpressResponseFromNativeError } from '@aliceo2/web-ui';
1516
import { isUserRoleSufficient } from './../../common/library/userRole.enum.js';
16-
import { UnauthorizedAccessError } from './../errors/UnauthorizedAccessError.js';
17-
import {
18-
updateExpressResponseFromNativeError,
19-
} from './../errors/updateExpressResponseFromNativeError.js';
2017

2118
/**
2219
* Method to receive a minimum role that needs to be met by owner of request and to return a middleware function
@@ -44,14 +41,14 @@ export const minimumRoleMiddleware = (minimumRole) =>
4441
}
4542
const isAllowed = accessList.some((role) => isUserRoleSufficient(role, minimumRole));
4643
if (!isAllowed) {
47-
updateExpressResponseFromNativeError(
44+
updateAndSendExpressResponseFromNativeError(
4845
res,
4946
new UnauthorizedAccessError('Not enough permissions for this operation'),
5047
);
5148
} else {
5249
next();
5350
}
5451
} catch (error) {
55-
updateExpressResponseFromNativeError(res, error);
52+
updateAndSendExpressResponseFromNativeError(res, error);
5653
}
5754
};

QualityControl/lib/services/JsonFileService.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
* or submit itself to any jurisdiction.
1313
*/
1414

15-
import { LogManager } from '@aliceo2/web-ui';
15+
import { LogManager, NotFoundError } from '@aliceo2/web-ui';
1616
const logger = LogManager.getLogger(`${process.env.npm_config_log_label ?? 'qcg'}/json`);
1717
import fs from 'fs';
1818
import path from 'path';
19-
import { NotFoundError } from './../errors/NotFoundError.js';
2019

2120
/**
2221
* Store layouts inside JSON based file with atomic write

0 commit comments

Comments
 (0)