[FIX] Correct the fail retur API to transmit the error from backend when compatible and wrat it when error occured

This commit is contained in:
Edouard DUPIN 2024-05-31 19:52:00 +02:00
parent 91849094cd
commit 1fe3cc3523

View File

@ -4,7 +4,7 @@
* @license MPL-2 * @license MPL-2
*/ */
import { RestErrorResponse } from "./model"; import { RestErrorResponse, isRestErrorResponse } from "./model";
export enum HTTPRequestModel { export enum HTTPRequestModel {
DELETE = "DELETE", DELETE = "DELETE",
@ -303,9 +303,9 @@ export function RESTRequest({
name: "Model accept type incompatible", name: "Model accept type incompatible",
time: Date().toString(), time: Date().toString(),
status: 901, status: 901,
error: `REST check wrong type: ${restModel.accept} != ${contentType}`, message: `REST Content type are not compatible: ${restModel.accept} != ${contentType}`,
statusMessage: "Fetch error", statusMessage: "Fetch error",
message: "rest-tools.ts Wrong type in the message return type", error: "rest-tools.ts Wrong type in the message return type",
} as RestErrorResponse); } as RestErrorResponse);
} else if (contentType === HTTPMimeType.JSON) { } else if (contentType === HTTPMimeType.JSON) {
response response
@ -313,39 +313,92 @@ export function RESTRequest({
.then((value: any) => { .then((value: any) => {
resolve({ status: response.status, data: value }); resolve({ status: response.status, data: value });
}) })
.catch((reason: any) => { .catch((reason: Error) => {
reject({ reject({
name: "API serialization error", name: "API serialization error",
time: Date().toString(), time: Date().toString(),
status: 902, status: 902,
error: `REST parse json fail: ${reason}`, message: `REST parse json fail: ${reason}`,
statusMessage: "Fetch parse error", statusMessage: "Fetch parse error",
message: "rest-tools.ts Wrong message model to parse", error: "rest-tools.ts Wrong message model to parse",
} as RestErrorResponse); } as RestErrorResponse);
}); });
} else { } else {
resolve({ status: response.status, data: response.body }); resolve({ status: response.status, data: response.body });
} }
} else { } else {
// the answer is not correct not a 2XX
// clone the response to keep the raw data if case of error:
response
.clone()
.json()
.then((value: any) => {
if (isRestErrorResponse(value)) {
reject(value);
} else {
response
.text()
.then((dataError: string) => {
reject({ reject({
name: "REST return no OK status", name: "API serialization error",
time: Date().toString(),
status: 903,
message: `REST parse error json with wrong type fail. ${dataError}`,
statusMessage: "Fetch parse error",
error: "rest-tools.ts Wrong message model to parse",
} as RestErrorResponse);
})
.catch((reason: any) => {
reject({
name: "API serialization error",
time: Date().toString(), time: Date().toString(),
status: response.status, status: response.status,
error: `${response.body}`, message: `unmanaged error model: ??? with error: ${reason}`,
statusMessage: "Fetch code error", statusMessage: "Fetch ERROR parse error",
message: "rest-tools.ts Wrong return code", error: "rest-tools.ts Wrong message model to parse",
} as RestErrorResponse); } as RestErrorResponse);
});
} }
}) })
.catch((error: any) => { .catch((reason: Error) => {
response
.text()
.then((dataError: string) => {
reject({
name: "API serialization error",
time: Date().toString(),
status: response.status,
message: `unmanaged error model: ${dataError} with error: ${reason}`,
statusMessage: "Fetch ERROR TEXT parse error",
error: "rest-tools.ts Wrong message model to parse",
} as RestErrorResponse);
})
.catch((reason: any) => {
reject({
name: "API serialization error",
time: Date().toString(),
status: response.status,
message: `unmanaged error model: ??? with error: ${reason}`,
statusMessage: "Fetch ERROR TEXT FAIL",
error: "rest-tools.ts Wrong message model to parse",
} as RestErrorResponse);
});
});
}
})
.catch((error: Error) => {
if (isRestErrorResponse(error)) {
reject(error);
} else {
reject({ reject({
name: "Request fail", name: "Request fail",
time: Date(), time: Date(),
status: 999, status: 999,
error: error, message: error,
statusMessage: "Fetch catch error", statusMessage: "Fetch catch error",
message: "rest-tools.ts detect an error in the fetch request", error: "rest-tools.ts detect an error in the fetch request",
}); });
}
}); });
}); });
} }