diff --git a/apps/backend/src/route-handlers/smart-route-handler.tsx b/apps/backend/src/route-handlers/smart-route-handler.tsx index 4648a19f7..55d0c9e54 100644 --- a/apps/backend/src/route-handlers/smart-route-handler.tsx +++ b/apps/backend/src/route-handlers/smart-route-handler.tsx @@ -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)); diff --git a/packages/stack-shared/src/interface/clientInterface.ts b/packages/stack-shared/src/interface/clientInterface.ts index 7d7db0503..c0c4220e9 100644 --- a/packages/stack-shared/src/interface/clientInterface.ts +++ b/packages/stack-shared/src/interface/clientInterface.ts @@ -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); } } diff --git a/packages/stack-shared/src/interface/serverInterface.ts b/packages/stack-shared/src/interface/serverInterface.ts index c9699cc8f..b0862d559 100644 --- a/packages/stack-shared/src/interface/serverInterface.ts +++ b/packages/stack-shared/src/interface/serverInterface.ts @@ -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); } }