diff --git a/src/org/kar/archidata/catcher/RestErrorResponse.java b/src/org/kar/archidata/catcher/RestErrorResponse.java index 5d424a0..7b51a09 100644 --- a/src/org/kar/archidata/catcher/RestErrorResponse.java +++ b/src/org/kar/archidata/catcher/RestErrorResponse.java @@ -3,19 +3,21 @@ package org.kar.archidata.catcher; import java.time.Instant; import java.util.UUID; +import org.kar.archidata.tools.UuidUtils; + import jakarta.ws.rs.core.Response; public class RestErrorResponse { - public UUID uuid = UUID.randomUUID(); + public UUID uuid = UuidUtils.nextUUID(); + public String name; // Mandatory for TS generic error + public String message; // Mandatory for TS generic error public String time; - public String error; - public String message; final public int status; final public String statusMessage; public RestErrorResponse(final Response.Status status, final String time, final String error, final String message) { this.time = time; - this.error = error; + this.name = error; this.message = message; this.status = status.getStatusCode(); this.statusMessage = status.getReasonPhrase(); @@ -23,13 +25,15 @@ public class RestErrorResponse { public RestErrorResponse(final Response.Status status, final String error, final String message) { this.time = Instant.now().toString(); - this.error = error; + this.name = error; this.message = message; this.status = status.getStatusCode(); this.statusMessage = status.getReasonPhrase(); } public RestErrorResponse(final Response.Status status) { + this.name = "generic"; + this.message = ""; this.time = Instant.now().toString(); this.status = status.getStatusCode(); this.statusMessage = status.getReasonPhrase(); diff --git a/src/org/kar/archidata/dataAccess/DataFactoryZod.java b/src/org/kar/archidata/dataAccess/DataFactoryZod.java index 1459507..321c7c91 100644 --- a/src/org/kar/archidata/dataAccess/DataFactoryZod.java +++ b/src/org/kar/archidata/dataAccess/DataFactoryZod.java @@ -97,7 +97,7 @@ public class DataFactoryZod { out.append("zod.enum(["); for (final Object elem : arr) { if (!first) { - out.append(", \n\t"); + out.append(",\n\t"); } else { out.append("\n\t"); first = false; @@ -106,14 +106,18 @@ public class DataFactoryZod { out.append(elem.toString()); out.append("'"); } - out.append("\n\t])"); + if (first) { + out.append("]}"); + } else { + out.append("\n\t])"); + } } else { element.isEnum = true; boolean first = true; out.append("{"); for (final Object elem : arr) { if (!first) { - out.append(", \n\t"); + out.append(",\n\t"); } else { out.append("\n\t"); first = false; @@ -123,7 +127,11 @@ public class DataFactoryZod { out.append(elem.toString()); out.append("'"); } - out.append("}"); + if (first) { + out.append("}"); + } else { + out.append(",\n\t}"); + } } element.declaration = out.toString(); previous.addOrder(element); diff --git a/src/org/kar/archidata/exception/RESTErrorResponseExeption.java b/src/org/kar/archidata/exception/RESTErrorResponseExeption.java index 03b0717..780f7d1 100644 --- a/src/org/kar/archidata/exception/RESTErrorResponseExeption.java +++ b/src/org/kar/archidata/exception/RESTErrorResponseExeption.java @@ -5,7 +5,7 @@ import java.util.UUID; public class RESTErrorResponseExeption extends Exception { public UUID uuid; public String time; - public String error; + public String name; public String message; public int status; public String statusMessage; @@ -13,16 +13,16 @@ public class RESTErrorResponseExeption extends Exception { public RESTErrorResponseExeption() { this.uuid = null; this.time = null; - this.error = null; + this.name = null; this.message = null; this.status = 0; this.statusMessage = null; } - public RESTErrorResponseExeption(final UUID uuid, final String time, final String error, final String message, final int status, final String statusMessage) { + public RESTErrorResponseExeption(final UUID uuid, final String time, final String name, final String message, final int status, final String statusMessage) { this.uuid = uuid; this.time = time; - this.error = error; + this.name = name; this.message = message; this.status = status; this.statusMessage = statusMessage; @@ -30,7 +30,7 @@ public class RESTErrorResponseExeption extends Exception { @Override public String toString() { - return "RESTErrorResponseExeption [uuid=" + this.uuid + ", time=" + this.time + ", error=" + this.error + ", message=" + this.message + ", status=" + this.status + ", statusMessage=" + return "RESTErrorResponseExeption [uuid=" + this.uuid + ", time=" + this.time + ", name=" + this.name + ", message=" + this.message + ", status=" + this.status + ", statusMessage=" + this.statusMessage + "]"; } diff --git a/src/org/kar/archidata/tools/RESTApi.java b/src/org/kar/archidata/tools/RESTApi.java index 2ada911..38ea41d 100644 --- a/src/org/kar/archidata/tools/RESTApi.java +++ b/src/org/kar/archidata/tools/RESTApi.java @@ -47,7 +47,7 @@ public class RESTApi { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { try { final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), RESTErrorResponseExeption.class); - throw new RESTErrorResponseExeption(out.uuid, out.time, out.error, out.message, out.status, out.statusMessage); + throw out; } catch (final MismatchedInputException ex) { throw new IOException("Fail to get the data [" + httpResponse.statusCode() + "] " + httpResponse.body()); } @@ -123,7 +123,7 @@ public class RESTApi { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { try { final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), RESTErrorResponseExeption.class); - throw new RESTErrorResponseExeption(out.uuid, out.time, out.error, out.message, out.status, out.statusMessage); + throw out; } catch (final MismatchedInputException ex) { throw new IOException("Fail to get the data [" + httpResponse.statusCode() + "] " + httpResponse.body()); } catch (final JsonParseException ex) { @@ -156,7 +156,7 @@ public class RESTApi { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { try { final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), RESTErrorResponseExeption.class); - throw new RESTErrorResponseExeption(out.uuid, out.time, out.error, out.message, out.status, out.statusMessage); + throw out; } catch (final MismatchedInputException ex) { throw new IOException("Fail to get the data [" + httpResponse.statusCode() + "] " + httpResponse.body()); } @@ -178,7 +178,7 @@ public class RESTApi { if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) { try { final RESTErrorResponseExeption out = this.mapper.readValue(httpResponse.body(), RESTErrorResponseExeption.class); - throw new RESTErrorResponseExeption(out.uuid, out.time, out.error, out.message, out.status, out.statusMessage); + throw out; } catch (final MismatchedInputException ex) { throw new IOException("Fail to get the data [" + httpResponse.statusCode() + "] " + httpResponse.body()); } diff --git a/src/resources/rest-tools.ts b/src/resources/rest-tools.ts index 0879a22..5b0dbad 100644 --- a/src/resources/rest-tools.ts +++ b/src/resources/rest-tools.ts @@ -150,12 +150,12 @@ export function RESTRequest({ restModel, restConfig, data, params, queries }: RE const contentType = response.headers.get('Content-Type'); if (restModel.accept !== contentType) { reject({ + name: `REST check wrong type: ${restModel.accept} != ${contentType}`, + message: "rest-tools.ts Wrong type in the message return type", time: Date().toString(), status: 901, - error: `REST check wrong type: ${restModel.accept} != ${contentType}`, statusMessage: "Fetch error", - message: "rest-tools.ts Wrong type in the message return type" - } as RestErrorResponse); + }); } else if (contentType === HTTPMimeType.JSON) { response .json() @@ -165,32 +165,32 @@ export function RESTRequest({ restModel, restConfig, data, params, queries }: RE }) .catch((reason: any) => { reject({ + name: `REST parse json fail: ${reason}`, + message: "rest-tools.ts Wrong message model to parse", time: Date().toString(), status: 902, - error: `REST parse json fail: ${reason}`, statusMessage: "Fetch parse error", - message: "rest-tools.ts Wrong message model to parse" - } as RestErrorResponse); + }); }); } else { resolve({ status: response.status, data: response.body }); } } else { reject({ + name: `${response.body}`, + message: "rest-tools.ts Wrong return code", time: Date().toString(), status: response.status, - error: `${response.body}`, statusMessage: "Fetch code error", - message: "rest-tools.ts Wrong return code" - } as RestErrorResponse); + }); } }).catch((error: any) => { reject({ + name: error, + message: "http-wrapper.ts detect an error in the fetch request", time: Date(), status: 999, - error: error, - statusMessage: "Fetch catch error", - message: "http-wrapper.ts detect an error in the fetch request" + statusMessage: "Fetch catch error" }); }); }); @@ -203,12 +203,12 @@ export function RESTRequestJson(request: RESTRequestType, checker: (data: resolve(value.data); } else { reject({ + name: "REST Fail to verify the data", + message: "api.ts Check type as fail", time: Date().toString(), status: 950, - error: "REST Fail to verify the data", statusMessage: "API cast ERROR", - message: "api.ts Check type as fail" - } as RestErrorResponse); + }); } }).catch((reason: RestErrorResponse) => { reject(reason); @@ -222,12 +222,12 @@ export function RESTRequestJsonArray(request: RESTRequestType, checker: (d resolve(value.data); } else { reject({ + name: "REST Fail to verify the data", + message: "api.ts Check type as fail", time: Date().toString(), status: 950, - error: "REST Fail to verify the data", statusMessage: "API cast ERROR", - message: "api.ts Check type as fail" - } as RestErrorResponse); + }); } }).catch((reason: RestErrorResponse) => { reject(reason);