Skip to content

Commit

Permalink
Minor changes to error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
forgetso committed Dec 5, 2024
1 parent ead21e0 commit a3d2a38
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 20 deletions.
2 changes: 2 additions & 0 deletions dev/config/src/vite/vite.test.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { defineConfig } from "vitest/config";
import VitePluginCloseAndCopy from "./vite-plugin-close-and-copy.js";
import VitePluginSourcemapExclude from "./vite-plugin-sourcemap-exclude.js";
Expand Down Expand Up @@ -52,6 +53,7 @@ export default function () {
},
pool: "forks", // forks is slower than 'threads' but more compatible with low-level libs (e.g. bcrypt)
testTimeout: 10000,
//disableConsoleIntercept: true,
},
plugins: [
VitePluginSourcemapExclude({ excludeNodeModules: true }),
Expand Down
10 changes: 6 additions & 4 deletions packages/common/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ export abstract class ProsopoBaseError<
private logError(logger: Logger, logLevel: LogLevel, errorName?: string) {
const errorParams = { error: this.message, context: this.context };
const errorMessage = { errorType: errorName || this.name, errorParams };
if (logLevel === "debug") {
logger.debug(this.stack);
}
logger[logLevel](errorMessage);
}
}
Expand Down Expand Up @@ -181,23 +184,22 @@ export class ProsopoApiError extends ProsopoBaseError<ApiContextParams> {

export const unwrapError = (err: ProsopoApiError | SyntaxError | ZodError) => {
const code = "code" in err ? err.code : 400;
let message = err.message;
let message = i18next.t(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.key =
err.context.translationKey || err.translationKey || "API.UNKNOWN";
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;
Expand Down
14 changes: 14 additions & 0 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
// Copyright 2021-2024 Prosopo (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
export type ApiJsonError = {
message: string;
key?: string;
code: number;
};
4 changes: 3 additions & 1 deletion packages/locale/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Falscher CAPTCHA-Typ",
"UNAUTHORIZED_ORIGIN_URL": "Nicht autorisierte Ursprungs-URL",
"INVALID_SITE_KEY": "Ungültiger Site-Schlüssel",
"INVALID_IP": "Ungültige IP"
"INVALID_IP": "Ungültige IP",
"INVALID_URL": "Ungültiger URL",
"INVALID_BODY": "Ungültiger Körper"
},
"CLI": {
"PARAMETER_ERROR": "Ungültiger Parameter"
Expand Down
4 changes: 3 additions & 1 deletion packages/locale/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Incorrect CAPTCHA type",
"INVALID_SITE_KEY": "Invalid site key",
"UNAUTHORIZED_ORIGIN_URL": "Unauthorized origin URL",
"INVALID_IP": "Invalid IP"
"INVALID_IP": "Invalid IP",
"INVALID_URL": "Invalid URL",
"INVALID_BODY": "Invalid body"
},
"CLI": {
"PARAMETER_ERROR": "Invalid parameter"
Expand Down
4 changes: 3 additions & 1 deletion packages/locale/src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Tipo di CAPTCHA errato",
"UNAUTHORIZED_ORIGIN_URL": "URL de origen no autorizada",
"INVALID_SITE_KEY": "Clave de sitio no válida",
"INVALID_IP": "IP no válida"
"INVALID_IP": "IP no válida",
"INVALID_URL": "URL inválida",
"INVALID_BODY": "Cuerpo inválido"
},
"CLI": {
"PARAMETER_ERROR": "Parámetro inválido"
Expand Down
4 changes: 3 additions & 1 deletion packages/locale/src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Type de CAPTCHA incorrect",
"UNAUTHORIZED_ORIGIN_URL": "URL d'origine non autorisée",
"INVALID_SITE_KEY": "Clé de site non valide",
"INVALID_IP": "IP invalide"
"INVALID_IP": "IP invalide",
"INVALID_URL": "URL inválida",
"INVALID_BODY": "Cuerpo inválido"
},
"CLI": {
"PARAMETER_ERROR": "Paramètre invalide"
Expand Down
4 changes: 3 additions & 1 deletion packages/locale/src/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Tipo di CAPTCHA errato",
"UNAUTHORIZED_ORIGIN_URL": "URL di origine non autorizzato",
"INVALID_SITE_KEY": "Chiave del sito non valida",
"INVALID_IP": "IP non valido"
"INVALID_IP": "IP non valido",
"INVALID_URL": "URL non valido",
"INVALID_BODY": "Corpo non valido"
},
"CLI": {
"PARAMETER_ERROR": "Parametro non valido"
Expand Down
4 changes: 3 additions & 1 deletion packages/locale/src/locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Tipo de CAPTCHA incorreto",
"UNAUTHORIZED_ORIGIN_URL": "URL de origem não autorizada",
"INVALID_SITE_KEY": "Chave de site inválida",
"INVALID_IP": "IP inválido"
"INVALID_IP": "IP inválido",
"INVALID_URL": "URL inválido",
"INVALID_BODY": "Corpo inválido"
},
"CLI": {
"PARAMETER_ERROR": "Parâmetro inválido"
Expand Down
4 changes: 3 additions & 1 deletion packages/locale/src/locales/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@
"INCORRECT_CAPTCHA_TYPE": "Tipo de CAPTCHA incorreto",
"UNAUTHORIZED_ORIGIN_URL": "URL de origem não autorizada",
"INVALID_SITE_KEY": "Chave de site inválida",
"INVALID_IP": "IP inválido"
"INVALID_IP": "IP inválido",
"INVALID_URL": "URL inválido",
"INVALID_BODY": "Corpo inválido"
},
"CLI": {
"PARAMETER_ERROR": "Parâmetro inválido"
Expand Down
10 changes: 1 addition & 9 deletions packages/provider/src/api/captcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,21 @@ import {
type CaptchaSolutionResponse,
type DappUserSolutionResult,
GetFrictionlessCaptchaChallengeRequestBody,
type GetFrictionlessCaptchaResponse,
GetPowCaptchaChallengeRequestBody,
type GetPowCaptchaChallengeRequestBodyTypeOutput,
type GetPowCaptchaResponse,
type PowCaptchaSolutionResponse,
SubmitPowCaptchaSolutionBody,
type SubmitPowCaptchaSolutionBodyTypeOutput,
type TGetImageCaptchaChallengePathAndParams,
} from "@prosopo/types";
import {
FrictionlessToken,
FrictionlessTokenRecord,
} from "@prosopo/types-database";
import type { ProviderEnvironment } from "@prosopo/types-env";
import { flatten, version } from "@prosopo/util";
import { flatten } from "@prosopo/util";
import express, { type Router } from "express";
import { v4 as uuidv4 } from "uuid";
import { getBotScore } from "../tasks/detection/getBotScore.js";
import { Tasks } from "../tasks/tasks.js";
import { getIPAddress } from "../util.js";
import { handleErrors } from "./errorHandler.js";

const NO_IP_ADDRESS = "NO_IP_ADDRESS" as const;
const DEFAULT_FRICTIONLESS_THRESHOLD = 0.5;

/**
Expand Down

0 comments on commit a3d2a38

Please sign in to comment.