Skip to content

Commit

Permalink
Move the error unwrapper into common
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Dec 4, 2024
1 parent 505725b commit ead21e0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 42 deletions.
35 changes: 35 additions & 0 deletions packages/common/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

import { type TranslationKey, i18n as i18next } from "@prosopo/locale";
import { ZodError } from "zod";
import { type LogLevel, type Logger, getLoggerDefault } from "./index.js";
import type { ApiJsonError } from "./types.js";

type BaseErrorOptions<ContextType> = {
name?: string;
Expand Down Expand Up @@ -176,3 +178,36 @@ export class ProsopoApiError extends ProsopoBaseError<ApiContextParams> {
this.code = code;
}
}

export const unwrapError = (err: ProsopoApiError | SyntaxError | ZodError) => {
const code = "code" in err ? err.code : 400;
let message = err.message;
let jsonError: ApiJsonError = { code, message };
let statusMessage = err.message;
jsonError.message = message;
// unwrap the errors to get the actual error message
while (err instanceof ProsopoBaseError && err.context) {
// base error will not have a translation key
jsonError.code =
err.context.translationKey || err.translationKey || jsonError.code;
jsonError.message = err.message;
if (err.context.error) {
err = err.context.error;
} else {
break;
}
}

if (err instanceof ZodError) {
message = i18next.t("CAPTCHA.PARSE_ERROR");
statusMessage = message;
if (typeof err.message === "object") {
jsonError = err.message;
} else {
jsonError.message = JSON.parse(err.message);
}
}

jsonError.code = jsonError.code || code;
return { code, statusMessage, jsonError };
};
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from "./logger.js";
export * from "./array.js";
export * from "./hash.js";
export * from "./string.js";
export * from "./types.js";
4 changes: 4 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ApiJsonError = {
message: string;
code: number;
};
39 changes: 2 additions & 37 deletions packages/provider/src/api/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// We need the unused params to make express recognise this function as an error handler
import { type ProsopoApiError, ProsopoBaseError } from "@prosopo/common";
import { i18n as i18next } from "@prosopo/locale";
import type { ApiJsonError } from "@prosopo/types";
import { type ProsopoApiError, unwrapError } from "@prosopo/common";
import type { NextFunction, Request, Response } from "express";
import { ZodError } from "zod";
import type { ZodError } from "zod";

export const handleErrors = (
err: ProsopoApiError | SyntaxError | ZodError,
Expand All @@ -31,36 +29,3 @@ export const handleErrors = (
response.send({ error: jsonError });
response.end();
};

export const unwrapError = (err: ProsopoApiError | SyntaxError | ZodError) => {
const code = "code" in err ? err.code : 400;
let message = err.message;
let jsonError: ApiJsonError = { code, message };
let statusMessage = err.message;
jsonError.message = message;
// unwrap the errors to get the actual error message
while (err instanceof ProsopoBaseError && err.context) {
// base error will not have a translation key
jsonError.code =
err.context.translationKey || err.translationKey || jsonError.code;
jsonError.message = err.message;
if (err.context.error) {
err = err.context.error;
} else {
break;
}
}

if (err instanceof ZodError) {
message = i18next.t("CAPTCHA.PARSE_ERROR");
statusMessage = message;
if (typeof err.message === "object") {
jsonError = err.message;
} else {
jsonError.message = JSON.parse(err.message);
}
}

jsonError.code = jsonError.code || code;
return { code, statusMessage, jsonError };
};
6 changes: 1 addition & 5 deletions packages/types/src/provider/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import type { ApiJsonError } from "@prosopo/common";
import type { Address4, Address6 } from "ip-address";
import {
type ZodDefault,
Expand Down Expand Up @@ -230,11 +231,6 @@ export interface ProviderRegistered {
status: "Registered" | "Unregistered";
}

export type ApiJsonError = {
message: string;
code: number;
};

export interface ApiResponse {
[ApiParams.status]: string;
[ApiParams.error]?: ApiJsonError;
Expand Down

0 comments on commit ead21e0

Please sign in to comment.