refactor(auth): translate better-auth errors allowedHosts (#854)
Closes #852
This commit is contained in:
parent
f0ac9fe35c
commit
0351d5e0b9
2 changed files with 26 additions and 4 deletions
|
|
@ -86,14 +86,14 @@ export const createApp = () => {
|
|||
app.get("/api/v1/docs", requireAuth, scalarDescriptor);
|
||||
|
||||
app.onError((err, c) => {
|
||||
logger.error(`${c.req.url}: ${err.message}`);
|
||||
const { status, message, details } = handleServiceError(err);
|
||||
|
||||
logger.error(`${c.req.url}: ${message}`);
|
||||
|
||||
if (err.cause instanceof Error) {
|
||||
logger.error(err.cause.message);
|
||||
}
|
||||
|
||||
const { status, message, details } = handleServiceError(err);
|
||||
|
||||
return c.json(details ? { message, details } : { message }, status as 500);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,28 @@ import { sanitizeSensitiveData } from "@zerobyte/core/node";
|
|||
import { ResticError } from "@zerobyte/core/restic";
|
||||
import { toErrorDetails as getErrorDetails, toMessage as getMessage } from "@zerobyte/core/utils";
|
||||
|
||||
const formatAllowedHostsMessage = (message: string) => {
|
||||
const referencesAllowedHosts = /\ballowed\s+hosts?\b|\ballowedHosts\b/i.test(message);
|
||||
const referencesBaseUrlResolution = /\bbase\s*url\b|\bhost\b|\bfallback\s+URL\b/i.test(message);
|
||||
|
||||
if (!referencesAllowedHosts || !referencesBaseUrlResolution) {
|
||||
return message;
|
||||
}
|
||||
|
||||
const requestedHost = message.match(/\bHost\s+"([^"]+)"/i)?.[1];
|
||||
const configuredHosts = message.match(/\bAllowed\s+hosts:\s*(.*?)(?:\.\s+Add\b|$)/i)?.[1]?.trim();
|
||||
const hostDetails = [
|
||||
requestedHost ? `Requested host: ${requestedHost}.` : undefined,
|
||||
configuredHosts ? `Configured BASE_URL/TRUSTED_ORIGINS hosts: ${configuredHosts}.` : undefined,
|
||||
].filter(Boolean);
|
||||
|
||||
return [
|
||||
"Could not resolve the auth base URL from this request.",
|
||||
...hostDetails,
|
||||
"Set BASE_URL to the URL you use to access Zerobyte, or add this origin to TRUSTED_ORIGINS.",
|
||||
].join(" ");
|
||||
};
|
||||
|
||||
export const handleServiceError = (error: unknown) => {
|
||||
if (error instanceof HttpError) {
|
||||
return { message: sanitizeSensitiveData(error.message), status: error.statusCode };
|
||||
|
|
@ -16,7 +38,7 @@ export const handleServiceError = (error: unknown) => {
|
|||
};
|
||||
}
|
||||
|
||||
return { message: sanitizeSensitiveData(toMessage(error)), status: 500 as const };
|
||||
return { message: formatAllowedHostsMessage(sanitizeSensitiveData(toMessage(error))), status: 500 as const };
|
||||
};
|
||||
|
||||
export const toMessage = (err: unknown): string => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue