-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move the error unwrapper into common #1558
Changes from 7 commits
ead21e0
a3d2a38
d00a33c
5e09586
7a2cde3
0ddc99e
f046804
46fc678
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -70,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); | ||
} | ||
} | ||
|
@@ -176,3 +181,42 @@ export class ProsopoApiError extends ProsopoBaseError<ApiContextParams> { | |
this.code = code; | ||
} | ||
} | ||
|
||
export const unwrapError = (err: ProsopoApiError | SyntaxError | ZodError) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this typed as a ProsopoApiError? Aren't there other error types which we might want to unwrap? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the moment its only being used for ProsopoApiError, but yes it should probably be ProsopoBaseError. Changed. |
||
const code = "code" in err ? err.code : 400; | ||
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.key = | ||
err.context.translationKey || err.translationKey || "API.UNKNOWN"; | ||
jsonError.message = err.message; | ||
if (err.context.error) { | ||
err = err.context.error; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
if (isZodError(err)) { | ||
message = i18next.t("API.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 }; | ||
}; | ||
|
||
export function isZodError(err: unknown): err is ZodError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using consts or functions? different declaration in same file between line 184 and line 218 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've changed it to a const. Don't understand the second comment. What do you mean about a different declaration? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was trying to communicate: Don't mind function or const, both are fine. Just imo best to keep consistent within files, not use both |
||
return Boolean( | ||
err && (err instanceof ZodError || (err as ZodError).name === "ZodError"), | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove new unused comment