You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
939 B
34 lines
939 B
import { AuthConflictError, AuthFailedError, AuthValidationError } from ".";
|
|
|
|
function hasStatusCode(err: unknown): err is { statusCode: number } {
|
|
return typeof err === "object" && err !== null && "statusCode" in err
|
|
&& typeof (err as { statusCode?: unknown }).statusCode === "number";
|
|
}
|
|
|
|
export function toPublicAuthError(err: unknown) {
|
|
if (hasStatusCode(err)) {
|
|
return err;
|
|
}
|
|
if (err instanceof AuthValidationError) {
|
|
return createError({
|
|
statusCode: 400,
|
|
statusMessage: err.message,
|
|
});
|
|
}
|
|
if (err instanceof AuthFailedError) {
|
|
return createError({
|
|
statusCode: 401,
|
|
statusMessage: err.message,
|
|
});
|
|
}
|
|
if (err instanceof AuthConflictError) {
|
|
return createError({
|
|
statusCode: 409,
|
|
statusMessage: err.message,
|
|
});
|
|
}
|
|
return createError(err instanceof Error ? err : {
|
|
statusCode: 500,
|
|
statusMessage: "服务器繁忙,请稍后重试",
|
|
});
|
|
}
|
|
|