TWO_FACTOR_ENCRYPTION_KEY has no default, so instances upgrading to 3.3 without setting it hit an unhandled throw from encryptSecret: the setup dialog only reported "Something went wrong", and where 2FA was required the blocking dialog made the whole app unusable. The status endpoint now reports 2FA as not required while the key is missing or invalid, and setup initiation returns 503 with a two-factor-error-not-configured code that the setup dialog renders as a readable message. Also documents the variable in the README and the example compose file. Closes #4443
83 lines
1.9 KiB
TypeScript
83 lines
1.9 KiB
TypeScript
import { serializeError } from 'serialize-error';
|
|
|
|
export function ok() {
|
|
return Response.json({ ok: true });
|
|
}
|
|
|
|
export function json(data: Record<string, any> = {}) {
|
|
return Response.json(data);
|
|
}
|
|
|
|
export function badRequest(error?: Record<string, any>) {
|
|
return Response.json(
|
|
{
|
|
error: { message: 'Bad request', code: 'bad-request', status: 400, ...error },
|
|
},
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
|
|
export function unauthorized(error?: Record<string, any>) {
|
|
return Response.json(
|
|
{
|
|
error: {
|
|
message: 'Unauthorized',
|
|
code: 'unauthorized',
|
|
status: 401,
|
|
...error,
|
|
},
|
|
},
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
|
|
export function forbidden(error?: Record<string, any>) {
|
|
return Response.json(
|
|
{ error: { message: 'Forbidden', code: 'forbidden', status: 403, ...error } },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
|
|
export function payloadTooLarge(error?: Record<string, any>) {
|
|
return Response.json(
|
|
{
|
|
error: { message: 'Payload too large', code: 'payload-too-large', status: 413, ...error },
|
|
},
|
|
{ status: 413 },
|
|
);
|
|
}
|
|
|
|
export function notFound(error?: Record<string, any>) {
|
|
return Response.json(
|
|
{ error: { message: 'Not found', code: 'not-found', status: 404, ...error } },
|
|
{ status: 404 },
|
|
);
|
|
}
|
|
|
|
export function serviceUnavailable(error?: Record<string, any>) {
|
|
return Response.json(
|
|
{
|
|
error: { message: 'Service unavailable', code: 'service-unavailable', status: 503, ...error },
|
|
},
|
|
{ status: 503 },
|
|
);
|
|
}
|
|
|
|
export function serverError(error?: unknown) {
|
|
if (error && typeof error !== 'string') {
|
|
// eslint-disable-next-line no-console
|
|
console.log(serializeError(error));
|
|
}
|
|
|
|
return Response.json(
|
|
{
|
|
error: {
|
|
message: typeof error === 'string' ? error : 'Server error',
|
|
code: 'server-error',
|
|
status: 500,
|
|
},
|
|
},
|
|
{ status: 500 },
|
|
);
|
|
}
|