* Creates a new error with the specified message, properties, and/or inner error. * If an inner error is provided, then the new error will match its type, if possible.
(...args: unknown[])
| 20 | * If an inner error is provided, then the new error will match its type, if possible. |
| 21 | */ |
| 22 | function ono<E extends ErrorLike, P extends object>(...args: unknown[]) { |
| 23 | let originalError = args[0] as ErrorLike | undefined; |
| 24 | |
| 25 | // Is the first argument an Error-like object? |
| 26 | if (typeof originalError === "object" && typeof originalError.name === "string") { |
| 27 | |
| 28 | // Try to find an Ono singleton method that matches this error type |
| 29 | for (let typedOno of Object.values(onoMap)) { |
| 30 | if (typeof typedOno === "function" && typedOno.name === "ono") { |
| 31 | let species = typedOno[Symbol.species]; |
| 32 | |
| 33 | if (species && species !== Error && (originalError instanceof species || originalError.name === species.name)) { |
| 34 | // Create an error of the same type |
| 35 | return typedOno.apply(undefined, args); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // By default, create a base Error object |
| 42 | return ono.error.apply(undefined, args); |
| 43 | } |
no outgoing calls