add renderInlineError to serialize errors in a consistent way

This commit is contained in:
Hazelnoot 2025-03-03 01:03:21 -05:00
parent 3808502f86
commit 61d0aeba2e
4 changed files with 42 additions and 36 deletions

View file

@ -0,0 +1,35 @@
/*
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { IdentifiableError } from '@/misc/identifiable-error.js';
import { StatusError } from '@/misc/status-error.js';
export function renderInlineError(err: unknown): string {
if (err instanceof IdentifiableError) {
if (err.message) {
return `${err.name} ${err.id}: ${err.message}`;
} else {
return `${err.name} ${err.id}`;
}
}
if (err instanceof StatusError) {
if (err.message) {
return `${err.name} ${err.statusCode}: ${err.message}`;
} else {
return `${err.name} ${err.statusCode}`;
}
}
if (err instanceof Error) {
if (err.message) {
return `${err.name}: ${err.message}`;
} else {
return err.name;
}
}
return String(err);
}