NonError.ts
This file
Path
source/errors/NonError.ts
37 lines · 1022 bytes · TypeScript
Last changed by
RefactorSindre Sorhusbefore adoptiona5ed57b
History of this fileTake it
RefactorSindre Sorhusbefore adoptiona5ed57b
lines 1–2why, one step back1import {isError} from '../utils/is.js';2Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4
lines 3–8why, one step back3/**4Wrapper for non-Error values that were thrown.56In JavaScript, any value can be thrown (not just Error instances). This class wraps such values to ensure consistent error handling.7*/8export class NonError extends Error {RefactorSindre Sorhusbefore adoptiona5ed57b
lines 9–15why, one step back9 /**10 Returns the value as-is when it is an `Error`, including one from another realm, and otherwise wraps it in a `NonError`.11 */12 static wrap(value: unknown): Error {13 return isError(value) ? value : new NonError(value);14 }15Add `retryOnTimeout` and `shouldRetry` options (#767)Sindre Sorhusbefore adoptionf0fdbd4
lines 16–37why, one step back16 override name = 'NonError';17 readonly value: unknown;1819 constructor(value: unknown) {20 let message = 'Non-error value was thrown';2122 // Intentionally minimal as this error is just an edge-case.23 try {24 if (typeof value === 'string') {25 message = value;26 } else if (value && typeof value === 'object' && 'message' in value && typeof value.message === 'string') {27 message = value.message;28 }29 } catch {30 // Use default message if accessing properties throws31 }3233 super(message);3435 this.value = value;36 }37}