File tree Expand file tree Collapse file tree 2 files changed +11
-19
lines changed Expand file tree Collapse file tree 2 files changed +11
-19
lines changed Original file line number Diff line number Diff line change 1
1
export * from "./json.bundle.ts" ;
2
2
export * as json from "./json.bundle.ts" ;
3
- export { JsonParseException } from "./json.lib.ts" ;
Original file line number Diff line number Diff line change @@ -18,8 +18,14 @@ export const serialize = <SHAPE = unknown>(
18
18
data : SHAPE ,
19
19
config : { pretty ?: boolean ; schema ?: ZodSchema < any , SHAPE > } = { } ,
20
20
) => {
21
- const validatedData = config . schema ? config . schema . parse ( data ) : data ;
22
- return JSON . stringify ( validatedData , null , config . pretty ? 2 : undefined ) ;
21
+ try {
22
+ const validatedData = config . schema ? config . schema . parse ( data ) : data ;
23
+ return JSON . stringify ( validatedData , null , config . pretty ? 2 : undefined ) ;
24
+ } catch ( error ) {
25
+ throw new Error ( `Failed to serialize JSON` , {
26
+ cause : error ,
27
+ } ) ;
28
+ }
23
29
} ;
24
30
25
31
/**
@@ -39,22 +45,9 @@ export const deserialize = <SHAPE = unknown>(
39
45
if ( config . schema ) return config . schema . parse ( validatedData ) ;
40
46
return validatedData ;
41
47
} catch ( error ) {
42
- throw new JsonParseException ( content . toString ( ) , error ) ;
48
+ throw new Error ( `Failed to parse JSON` , {
49
+ cause : error ,
50
+ } ) ;
43
51
}
44
52
} ;
45
53
46
- export class JsonParseException extends Error {
47
- constructor ( value : string , cause ?: any ) {
48
- const message = `
49
- Failed to parse JSON
50
-
51
- [JSON String Value]:
52
- ${ value }
53
-
54
- [Cause]:
55
- ${ cause instanceof Error ? cause . message : String ( cause ) }
56
- ` ;
57
-
58
- super ( message , { cause } ) ;
59
- }
60
- }
You can’t perform that action at this time.
0 commit comments