More instanceof replacements

This commit is contained in:
Konstantin Wohlwend 2025-05-15 12:18:34 -07:00
parent 8717a70adc
commit c93393efa1
3 changed files with 12 additions and 12 deletions

View File

@ -23,14 +23,14 @@ class InternalServerError extends StatusError {
}
/**
* Known errors that are common and should not be logged with their stacktrace.
* Some errors that are common and should not be logged with their stacktrace.
*/
const commonErrors = [
...getNodeEnvironment() === "development" ? [KnownError] : [],
KnownErrors.AccessTokenExpired,
KnownErrors.CannotGetOwnUserWithoutUser,
InternalServerError,
];
function isCommonError(error: unknown): boolean {
return KnownError.isKnownError(error)
|| error instanceof InternalServerError
|| KnownErrors.AccessTokenExpired.isInstance(error)
|| KnownErrors.CannotGetOwnUserWithoutUser.isInstance(error);
}
/**
* Catches the given error, logs it if needed and returns it as a StatusError. Errors that are not actually errors
@ -130,7 +130,7 @@ export function handleApiRequest(handler: (req: NextRequest, options: any, reque
if (!disableExtendedLogging) console.log(`[ ERR] [${requestId}] ${req.method} ${req.url}: ${statusError.message}`);
if (!commonErrors.some(e => statusError instanceof e)) {
if (!isCommonError(statusError)) {
// HACK: Log a nicified version of the error instead of statusError to get around buggy Next.js pretty-printing
// https://www.reddit.com/r/nextjs/comments/1gkxdqe/comment/m19kxgn/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
if (!disableExtendedLogging) console.debug(`For the error above with request ID ${requestId}, the full error is:`, errorToNiceString(statusError));

View File

@ -229,7 +229,7 @@ export class StackClientInterface {
return Result.ok(await this.sendClientRequest(path, requestOptions, tokenStoreOrNull));
} catch (e) {
for (const errorType of errorsToCatch) {
if (e instanceof errorType) {
if (errorType.isInstance(e)) {
return Result.error(e as InstanceType<E>);
}
}

View File

@ -5,8 +5,8 @@ import { filterUndefined } from "../utils/objects";
import { Result } from "../utils/results";
import { urlString } from "../utils/urls";
import {
ClientInterfaceOptions,
StackClientInterface
ClientInterfaceOptions,
StackClientInterface
} from "./clientInterface";
import { ContactChannelsCrud } from "./crud/contact-channels";
import { CurrentUserCrud } from "./crud/current-user";
@ -70,7 +70,7 @@ export class StackServerInterface extends StackClientInterface {
return Result.ok(await this.sendServerRequest(path, requestOptions, tokenStoreOrNull));
} catch (e) {
for (const errorType of errorsToCatch) {
if (e instanceof errorType) {
if (errorType.isInstance(e)) {
return Result.error(e as InstanceType<E>);
}
}