Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid error class name mangling in webpack #4768

Merged
merged 4 commits into from
Dec 6, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Construct error to get the real name property"
This reverts commit 6b2473e.
fregante committed Dec 6, 2022
commit 0638e3963375431a95601669bb76b35126eb7d84
44 changes: 13 additions & 31 deletions src/errors/errorHelpers.ts
Original file line number Diff line number Diff line change
@@ -18,16 +18,14 @@
import { deserializeError, type ErrorObject } from "serialize-error";
import { isObject, matchesAnyPattern, smartAppendPeriod } from "@/utils";
import safeJsonStringify from "json-stringify-safe";
import { isEmpty, memoize, truncate } from "lodash";
import { isEmpty, truncate } from "lodash";
import {
isAxiosError,
selectNetworkErrorMessage,
selectServerErrorMessage,
} from "@/errors/networkErrorHelpers";
import { type MessageContext } from "@/core";

type ErrorConstructor = new (...args: unknown[]) => Error;

// From "webext-messenger". Cannot import because the webextension polyfill can only run in an extension context
// TODO: https://github.com/pixiebrix/pixiebrix-extension/issues/3641
const errorTargetClosedEarly =
@@ -100,33 +98,25 @@ export function isErrorObject(error: unknown): error is ErrorObject {
return typeof (error as any)?.message === "string";
}

export function isSpecificError<ErrorType extends ErrorConstructor>(
error: unknown,
errorType: ErrorType
): error is InstanceType<ErrorType> {
if (!isErrorObject(error)) {
return false;
}

const errorTypeName = getErrorName(errorType);

export function isSpecificError<
ErrorType extends new (...args: unknown[]) => Error
>(error: unknown, errorType: ErrorType): error is InstanceType<ErrorType> {
// Catch 2 common error subclass groups. Necessary until we drop support for serialized errors:
// https://github.com/sindresorhus/serialize-error/issues/72
if (errorTypeName === "ClientRequestError") {
if (errorType.name === "ClientRequestError") {
return isClientRequestError(error);
}

if (errorTypeName === "BusinessError") {
if (errorType.name === "BusinessError") {
return isBusinessError(error);
}

return errorTypeName === error.name;
return isErrorObject(error) && error.name === errorType.name;
}

export function selectSpecificError<ErrorType extends ErrorConstructor>(
error: unknown,
errorType: ErrorType
): InstanceType<ErrorType> | null {
export function selectSpecificError<
ErrorType extends new (...args: unknown[]) => Error
>(error: unknown, errorType: ErrorType): InstanceType<ErrorType> | null {
if (!isObject(error)) {
return null;
}
@@ -151,17 +141,9 @@ export function getRootCause(error: unknown): unknown {
return error;
}

const getErrorName = memoize(
(ErrorOrErrorConstructor: Error | ErrorConstructor): string =>
ErrorOrErrorConstructor instanceof Error
? ErrorOrErrorConstructor.name
: new ErrorOrErrorConstructor().name
);

export function hasSpecificErrorCause<ErrorType extends ErrorConstructor>(
error: unknown,
errorType: ErrorType
): boolean {
export function hasSpecificErrorCause<
ErrorType extends new (...args: unknown[]) => Error
>(error: unknown, errorType: ErrorType): boolean {
return Boolean(selectSpecificError(error, errorType));
}