@@ -23,37 +23,38 @@ const NeptuneErrorSchema = z.object({
2323 */
2424export async function decodeErrorSafely ( response : Response ) : Promise < any > {
2525 const contentType = response . headers . get ( "Content-Type" ) ;
26- const contentTypeHasValue = contentType !== null && contentType . length > 0 ;
27- // Assume missing content type is JSON
28- const isJson =
29- ! contentTypeHasValue || contentType . includes ( "application/json" ) ;
30- const isHtml = contentTypeHasValue && contentType . includes ( "text/html" ) ;
26+ if (
27+ ! contentType ||
28+ ( ! contentType . includes ( "application/json" ) &&
29+ ! contentType . includes ( "text/plain" ) )
30+ ) {
31+ return undefined ;
32+ }
3133
3234 // Extract the raw text from the response
3335 const rawText = await response . text ( ) ;
34-
35- // Check for empty response
3636 if ( ! rawText ) {
3737 return undefined ;
38- } else if ( isJson ) {
38+ }
39+
40+ // Parse JSON
41+ if ( contentType . includes ( "application/json" ) ) {
3942 try {
4043 // Try parsing the response as JSON
4144 const data = JSON . parse ( rawText ) ;
4245
4346 // Flatten the error if it contains an error object
4447 return data ?. error ?? data ;
4548 } catch ( error ) {
46- console . error ( "Failed to decode the error response as JSON" , {
49+ // Log the error and fallthrough to return the raw text
50+ logger . warn ( "Failed to decode the error response as JSON" , {
4751 error,
4852 rawText,
4953 } ) ;
50- return rawText ;
5154 }
52- } else if ( isHtml ) {
53- // Ignore the content of HTML responses
54- return undefined ;
5555 }
5656
57+ // Just return the whole response as the error
5758 return { message : rawText } ;
5859}
5960
0 commit comments