File tree 2 files changed +26
-1
lines changed
2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @squarecloud/api " : patch
3
+ ---
4
+
5
+ Better request JSON parse error debugging.
Original file line number Diff line number Diff line change 1
1
import type { APIVersion } from "@squarecloud/api-types/v2" ;
2
2
3
+ import { existsSync } from "node:fs" ;
4
+ import { mkdir , writeFile } from "node:fs/promises" ;
3
5
import { SquareCloudAPIError } from "@/structures" ;
4
6
import type {
5
7
APIEndpoint ,
@@ -25,7 +27,9 @@ export class APIService {
25
27
const response = await fetch ( url , init ) . catch ( ( err ) => {
26
28
throw new SquareCloudAPIError ( err . code , err . message ) ;
27
29
} ) ;
28
- const data = await response . json ( ) ;
30
+ const data = await response
31
+ . json ( )
32
+ . catch ( ( ) => this . handleParseError ( response ) ) ;
29
33
30
34
if ( ! data || data . status === "error" || ! response . ok ) {
31
35
throw new SquareCloudAPIError ( data ?. code || "COMMON_ERROR" ) ;
@@ -65,4 +69,20 @@ export class APIService {
65
69
66
70
return { url, init } ;
67
71
}
72
+
73
+ private async handleParseError ( response : Response ) {
74
+ const text = await response . text ( ) ;
75
+ const dir = ".squarecloud/logs/" ;
76
+ const path = `${ dir } ${ this . userId } -${ Date . now ( ) } .log` ;
77
+
78
+ if ( ! existsSync ( dir ) ) {
79
+ await mkdir ( dir ) ;
80
+ }
81
+ await writeFile ( path , text ) ;
82
+
83
+ throw new SquareCloudAPIError (
84
+ "CANNOT_PARSE_JSON" ,
85
+ `Saved request text content to ${ path } ` ,
86
+ ) ;
87
+ }
68
88
}
You can’t perform that action at this time.
0 commit comments