add more details to StatusErrors

This commit is contained in:
Hazelnoot 2025-05-22 10:53:38 -04:00
parent e9eaafae41
commit 6627e8a9b8
4 changed files with 7 additions and 5 deletions

View file

@ -101,7 +101,7 @@ export class DownloadService {
await stream.pipeline(req, fs.createWriteStream(path));
} catch (e) {
if (e instanceof Got.HTTPError) {
throw new StatusError(`${e.response.statusCode} ${e.response.statusMessage}`, e.response.statusCode, e.response.statusMessage, e);
throw new StatusError(`download error from ${url}`, e.response.statusCode, e.response.statusMessage, e);
} else if (e instanceof Got.RequestError || e instanceof Got.AbortError) {
throw new Error(String(e), { cause: e });
} else if (e instanceof Error) {

View file

@ -331,7 +331,7 @@ export class HttpRequestService {
});
if (!res.ok && extra.throwErrorWhenResponseNotOk) {
throw new StatusError(`${res.status} ${res.statusText}`, res.status, res.statusText);
throw new StatusError(`request error from ${url}`, res.status, res.statusText);
}
if (res.ok) {

View file

@ -18,6 +18,8 @@ export function renderInlineError(err: unknown): string {
if (err instanceof StatusError) {
if (err.message) {
return `${err.name} ${err.statusCode}: ${err.message}`;
} else if (err.statusMessage) {
return `${err.name} ${err.statusCode}: ${err.statusMessage}`;
} else {
return `${err.name} ${err.statusCode}`;
}

View file

@ -384,7 +384,7 @@ export class FileServerService {
) {
if (!isConvertibleImage) {
// 画像でないなら404でお茶を濁す
throw new StatusError('Unexpected mime', 404);
throw new StatusError(`Unexpected non-convertible mime: ${file.mime}`, 404, 'Unexpected mime');
}
}
@ -448,7 +448,7 @@ export class FileServerService {
} else if (file.mime === 'image/svg+xml') {
image = this.imageProcessingService.convertToWebpStream(file.path, 2048, 2048);
} else if (!file.mime.startsWith('image/') || !FILE_TYPE_BROWSERSAFE.includes(file.mime)) {
throw new StatusError('Rejected type', 403, 'Rejected type');
throw new StatusError(`Blocked mime type: ${file.mime}`, 403, 'Blocked mime type');
}
if (!image) {
@ -522,7 +522,7 @@ export class FileServerService {
> {
if (url.startsWith(`${this.config.url}/files/`)) {
const key = url.replace(`${this.config.url}/files/`, '').split('/').shift();
if (!key) throw new StatusError('Invalid File Key', 400, 'Invalid File Key');
if (!key) throw new StatusError(`Invalid file URL ${url}`, 400, 'Invalid file url');
return await this.getFileFromKey(key);
}