File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
Expand file tree Collapse file tree 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 11import type { APIVersion } from "@squarecloud/api-types/v2" ;
22
3+ import { existsSync } from "node:fs" ;
4+ import { mkdir , writeFile } from "node:fs/promises" ;
35import { SquareCloudAPIError } from "@/structures" ;
46import type {
57 APIEndpoint ,
@@ -25,7 +27,9 @@ export class APIService {
2527 const response = await fetch ( url , init ) . catch ( ( err ) => {
2628 throw new SquareCloudAPIError ( err . code , err . message ) ;
2729 } ) ;
28- const data = await response . json ( ) ;
30+ const data = await response
31+ . json ( )
32+ . catch ( ( ) => this . handleParseError ( response ) ) ;
2933
3034 if ( ! data || data . status === "error" || ! response . ok ) {
3135 throw new SquareCloudAPIError ( data ?. code || "COMMON_ERROR" ) ;
@@ -65,4 +69,20 @@ export class APIService {
6569
6670 return { url, init } ;
6771 }
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+ }
6888}
You can’t perform that action at this time.
0 commit comments