fix: settings errors

This commit is contained in:
diced
2026-03-03 15:22:23 -08:00
parent 5d48735dfb
commit 4735b102c3
2 changed files with 23 additions and 5 deletions
+16 -1
View File
@@ -121,27 +121,42 @@ export type ApiErrorPayload = {
error: string;
code: ApiErrorCode;
statusCode: number;
[key: string]: any;
};
export class ApiError extends Error {
public readonly code: ApiErrorCode;
public readonly status: number;
public additional: Record<string, any>;
constructor(code: ApiErrorCode, message?: string, status?: number) {
super(message ?? API_ERRORS[code] ?? 'Unknown API error');
this.code = code;
this.status = status ?? ApiError.codeToHttpStatus(code);
this.additional = {} as Record<string, any>;
Object.setPrototypeOf(this, new.target.prototype);
}
add(key: string, value: any): this {
this.additional[key] = value;
return this;
}
toJSON(): ApiErrorPayload {
const formattedMessage = API_ERRORS[this.code]
? `${this.code}${this.message ? `: ${this.message}` : ''}`
: this.message;
return { error: formattedMessage, code: this.code, statusCode: this.status };
return {
error: formattedMessage,
code: this.code,
statusCode: this.status,
...this.additional,
};
}
public static check(payload: ApiErrorPayload, code: ApiErrorCode): boolean {
@@ -9,6 +9,7 @@ import { prisma } from '@/lib/db';
import { log } from '@/lib/logger';
import { secondlyRatelimit } from '@/lib/ratelimits';
import { readThemes } from '@/lib/theme/file';
import { zStringTrimmed } from '@/lib/validation';
import { administratorMiddleware } from '@/server/middleware/administrator';
import { userMiddleware } from '@/server/middleware/user';
import typedPlugin from '@/server/typedPlugin';
@@ -48,8 +49,11 @@ const jsonTransform = (value: any, ctx: z.RefinementCtx) => {
}
};
const zMs = z.string().refine((value) => ms(value as StringValue) > 0, 'Value must be greater than 0');
const zBytes = z.string().refine((value) => bytes(value) > 0, 'Value must be greater than 0');
const zMs = zStringTrimmed.refine(
(value) => ms((value ?? '0') as StringValue) > 0,
'Value must be greater than 0',
);
const zBytes = zStringTrimmed.refine((value) => bytes(value) > 0, 'Value must be greater than 0');
const zIntervalMs = zMs.refine(
(value) => ms(value as StringValue) <= MAX_SAFE_TIMEOUT_MS,
@@ -475,8 +479,7 @@ export default typedPlugin(
issues: result.error.issues,
});
// tODO: bring back issues field
throw new ApiError(1022);
throw new ApiError(1022).add('issues', result.error.issues);
}
const newSettings = await prisma.zipline.update({