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.
30 lines
910 B
30 lines
910 B
import { ConfigKeyNotFoundError, ConfigScopeInvalidError, ConfigValueInvalidError } 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 toPublicConfigError(err: unknown) {
|
|
if (hasStatusCode(err)) {
|
|
return err;
|
|
}
|
|
if (err instanceof ConfigKeyNotFoundError) {
|
|
return createError({
|
|
statusCode: 404,
|
|
statusMessage: err.message,
|
|
data: { code: err.code },
|
|
});
|
|
}
|
|
if (err instanceof ConfigScopeInvalidError || err instanceof ConfigValueInvalidError) {
|
|
return createError({
|
|
statusCode: 400,
|
|
statusMessage: err.message,
|
|
data: { code: err.code },
|
|
});
|
|
}
|
|
return createError({
|
|
statusCode: 500,
|
|
statusMessage: "服务器繁忙,请稍后重试",
|
|
});
|
|
}
|
|
|