@@ -12,6 +12,20 @@ export const dynamic = 'force-dynamic'
1212
1313const logger = createLogger ( 'JsmRequestAPI' )
1414
15+ function parseJsmErrorMessage ( status : number , statusText : string , errorText : string ) : string {
16+ try {
17+ const errorData = JSON . parse ( errorText )
18+ if ( errorData . errorMessage ) {
19+ return `JSM API error: ${ errorData . errorMessage } `
20+ }
21+ } catch {
22+ if ( errorText ) {
23+ return `JSM API error: ${ errorText } `
24+ }
25+ }
26+ return `JSM API error: ${ status } ${ statusText } `
27+ }
28+
1529export async function POST ( request : NextRequest ) {
1630 const auth = await checkInternalAuth ( request )
1731 if ( ! auth . success || ! auth . userId ) {
@@ -127,20 +141,11 @@ export async function POST(request: NextRequest) {
127141 error : errorText ,
128142 } )
129143
130- let errorMessage = `JSM API error: ${ response . status } ${ response . statusText } `
131- try {
132- const errorData = JSON . parse ( errorText )
133- if ( errorData . errorMessage ) {
134- errorMessage = `JSM API error: ${ errorData . errorMessage } `
135- }
136- } catch {
137- if ( errorText ) {
138- errorMessage = `JSM API error: ${ errorText } `
139- }
140- }
141-
142144 return NextResponse . json (
143- { error : errorMessage , details : errorText } ,
145+ {
146+ error : parseJsmErrorMessage ( response . status , response . statusText , errorText ) ,
147+ details : errorText ,
148+ } ,
144149 { status : response . status }
145150 )
146151 }
@@ -205,20 +210,11 @@ export async function POST(request: NextRequest) {
205210 error : errorText ,
206211 } )
207212
208- let errorMessage = `JSM API error: ${ response . status } ${ response . statusText } `
209- try {
210- const errorData = JSON . parse ( errorText )
211- if ( errorData . errorMessage ) {
212- errorMessage = `JSM API error: ${ errorData . errorMessage } `
213- }
214- } catch {
215- if ( errorText ) {
216- errorMessage = `JSM API error: ${ errorText } `
217- }
218- }
219-
220213 return NextResponse . json (
221- { error : errorMessage , details : errorText } ,
214+ {
215+ error : parseJsmErrorMessage ( response . status , response . statusText , errorText ) ,
216+ details : errorText ,
217+ } ,
222218 { status : response . status }
223219 )
224220 }
0 commit comments