1
1
import { extendError } from "./extend-error" ;
2
2
import { normalizeArgs , normalizeOptions } from "./normalize" ;
3
3
import { toJSON as errorToJSON } from "./to-json" ;
4
- import { ErrorLike , ErrorLikeConstructor , ErrorLikeConstructorClass , ErrorPOJO , OnoConstructor , OnoError , OnoOptions } from "./types" ;
4
+ import { ErrorLike , ErrorLikeConstructor , ErrorLikeConstructorClass , OnoConstructor , OnoOptions } from "./types" ;
5
5
6
6
const constructor = Ono as OnoConstructor ;
7
7
export { constructor as Ono } ;
8
8
9
- /**
10
- * Returns an object containing all properties of the given Error object,
11
- * which can be used with `JSON.stringify()`.
12
- */
13
- Ono . toJSON = function toJSON < E extends ErrorLike > ( error : E ) {
14
- return errorToJSON . call ( error ) as ErrorPOJO & E ;
15
- } ;
16
-
17
9
/**
18
10
* Creates an `Ono` instance for a specifc error type.
19
11
*/
@@ -25,14 +17,36 @@ function Ono<T extends ErrorLike>(ErrorConstructor: ErrorLikeConstructor<T>, opt
25
17
let { originalError, props, message } = normalizeArgs < E , P > ( args , options ! ) ;
26
18
27
19
// Create a new error of the specified type
28
- let newError = new ( ErrorConstructor as ErrorLikeConstructorClass < T > ) ( message ) as T & E & P & OnoError < T & E & P > ;
20
+ let newError = new ( ErrorConstructor as ErrorLikeConstructorClass < T > ) ( message ) ;
29
21
30
22
// Extend the error with the properties of the original error and the `props` object
31
- extendError ( newError , originalError , props ) ;
32
-
33
- return newError ;
23
+ return extendError ( newError , originalError , props ) ;
34
24
}
35
25
36
26
ono [ Symbol . species ] = ErrorConstructor ;
37
27
return ono ;
38
28
}
29
+
30
+ /**
31
+ * Returns an object containing all properties of the given Error object,
32
+ * which can be used with `JSON.stringify()`.
33
+ */
34
+ Ono . toJSON = function toJSON ( error : ErrorLike ) {
35
+ return errorToJSON . call ( error ) ;
36
+ } ;
37
+
38
+ /**
39
+ * Extends the given Error object with enhanced Ono functionality, such as nested stack traces,
40
+ * additional properties, and improved support for `JSON.stringify()`.
41
+ */
42
+ Ono . extend = function extend ( error : ErrorLike , originalError ?: ErrorLike , props ?: object ) {
43
+ if ( props || originalError instanceof Error ) {
44
+ return extendError ( error , originalError , props ) ;
45
+ }
46
+ else if ( originalError ) {
47
+ return extendError ( error , undefined , originalError ) ;
48
+ }
49
+ else {
50
+ return extendError ( error ) ;
51
+ }
52
+ } ;
0 commit comments