ky
Version:
Tiny and elegant HTTP client based on the Fetch API
34 lines (33 loc) • 1.16 kB
JavaScript
import { isError } from '../utils/is.js';
/**
Wrapper for non-Error values that were thrown.
In JavaScript, any value can be thrown (not just Error instances). This class wraps such values to ensure consistent error handling.
*/
export class NonError extends Error {
/**
Returns the value as-is when it is an `Error`, including one from another realm, and otherwise wraps it in a `NonError`.
*/
static wrap(value) {
return isError(value) ? value : new NonError(value);
}
name = 'NonError';
value;
constructor(value) {
let message = 'Non-error value was thrown';
// Intentionally minimal as this error is just an edge-case.
try {
if (typeof value === 'string') {
message = value;
}
else if (value && typeof value === 'object' && 'message' in value && typeof value.message === 'string') {
message = value.message;
}
}
catch {
// Use default message if accessing properties throws
}
super(message);
this.value = value;
}
}
//# sourceMappingURL=NonError.js.map