Skip to content

Commit eaa6256

Browse files
author
Andrea Cosentino
committed
fix: errors throwing logic
1 parent 1fb577c commit eaa6256

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

src/utils/plugin.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
196196
Utils.request.applyPaginationAndFilters(request, paginationHandler, filtersHandler, paginationPlugin, filtersPlugin, dataFile);
197197
} catch (error: any) {
198198
if (error instanceof UniversalApiError) {
199-
if (error.getType() === "MANUALLY_HANDLED") {
200-
error.setType("ERROR");
199+
if (!error.getPath()) {
201200
error.setPath(fullUrl.pathname);
202201
}
203202
throw error;
@@ -271,8 +270,7 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
271270
Utils.request.applyPaginationAndFilters(request, paginationHandler, filtersHandler, paginationPlugin, filtersPlugin, dataFile);
272271
} catch (error: any) {
273272
if (error instanceof UniversalApiError) {
274-
if (error.getType() === "MANUALLY_HANDLED") {
275-
error.setType("ERROR");
273+
if (!error.getPath()) {
276274
error.setPath(fullUrl.pathname);
277275
}
278276
throw error;
@@ -376,8 +374,7 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
376374
await Utils.files.writingFile(file, fileFound, newData, dataFile.mimeType, true);
377375
} catch (error: any) {
378376
if (error instanceof UniversalApiError) {
379-
if (error.getType() === "MANUALLY_HANDLED") {
380-
error.setType("ERROR");
377+
if (!error.getPath()) {
381378
error.setPath(fullUrl.pathname);
382379
}
383380
throw error;
@@ -412,8 +409,7 @@ async function handlingApiFsRequest(logger: ILogger, fullUrl: URL, request: Univ
412409
}
413410
} catch (error: any) {
414411
if (error instanceof UniversalApiError) {
415-
if (error.getType() === "MANUALLY_HANDLED") {
416-
error.setType("ERROR");
412+
if (!error.getPath()) {
417413
error.setPath(fullUrl.pathname);
418414
}
419415
throw error;

src/utils/utils.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function patchWalkPath(target: any, path: string) {
1515
if (path === '' || path === '/') {
1616
throw new UniversalApiError(
1717
"PATCH body request malformed: cannot use root path for this operation",
18-
"MANUALLY_HANDLED",
18+
"ERROR",
1919
"",
2020
Constants.HTTP_STATUS_CODE.BAD_REQUEST
2121
);
@@ -26,7 +26,7 @@ function patchWalkPath(target: any, path: string) {
2626
for (let i = 0; i < segments.length - 1; i++) {
2727
const key = segments[i];
2828
if (!(key in current)) {
29-
throw new UniversalApiError("PATCH body request malformed", "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
29+
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
3030
}
3131
current = current[key];
3232
}
@@ -270,13 +270,13 @@ export const Utils = {
270270
} else {
271271
// INFO RFC 6902 standard (JSON Patch)
272272
if (!Array.isArray(patch)) {
273-
throw new UniversalApiError("PATCH body request malformed", "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
273+
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
274274
}
275275
result = Utils.plugin.cloneData(data);
276276
patch.forEach(operation => {
277277
const { op, path, value, from } = operation;
278278
if (!op || !path) {
279-
throw new UniversalApiError("PATCH body request malformed", "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
279+
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
280280
}
281281
switch (op) {
282282
case 'add': {
@@ -295,7 +295,7 @@ export const Utils = {
295295
parent.splice(parseInt(key), 1);
296296
} else {
297297
if (!(key in parent)) {
298-
throw new UniversalApiError("PATCH body request malformed", "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
298+
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
299299
};
300300
delete parent[key];
301301
}
@@ -304,7 +304,7 @@ export const Utils = {
304304
case 'replace': {
305305
const { parent, key } = patchWalkPath(result, path);
306306
if (!(key in parent)) {
307-
throw new UniversalApiError("PATCH body request malformed", "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
307+
throw new UniversalApiError("PATCH body request malformed", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
308308
};
309309
parent[key] = value;
310310
break;
@@ -338,7 +338,7 @@ export const Utils = {
338338
break;
339339
}
340340
default:
341-
throw new UniversalApiError(`PATCH operation not supported: ${op}`, "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
341+
throw new UniversalApiError(`PATCH operation not supported: ${op}`, "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
342342
}
343343
})
344344
}
@@ -1038,7 +1038,7 @@ export const Utils = {
10381038
if (pagination !== null && Array.isArray(dataFile.data)) {
10391039
if (pagination.sort !== null && pagination.order !== null) {
10401040
if (!["ASC", "DESC", "1", "-1", "true", "false"].includes(pagination.order)) {
1041-
throw new UniversalApiError("Error parsing pagination request", "MANUALLY_HANDLED", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
1041+
throw new UniversalApiError("Error parsing pagination request", "ERROR", "", Constants.HTTP_STATUS_CODE.BAD_REQUEST);
10421042
}
10431043
dataFile.data = dataFile.data.sort((a: any, b: any) => {
10441044
return ["ASC", "1", "true"].includes(pagination.order!)

0 commit comments

Comments
 (0)