@helia/verified-fetch 2.6.3 → 2.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -98,17 +98,35 @@ export function notFoundResponse (url: string, body?: SupportedBodyTypes, init?:
98
98
  return response
99
99
  }
100
100
 
101
- /**
102
- * if body is an Error, it will be converted to a string containing the error message.
103
- */
104
- export function badRequestResponse (url: string, body?: SupportedBodyTypes | Error, init?: ResponseInit): Response {
105
- if (body instanceof Error) {
106
- body = body.message
101
+ function isArrayOfErrors (body: unknown | Error | Error[]): body is Error[] {
102
+ return Array.isArray(body) && body.every(e => e instanceof Error)
103
+ }
104
+
105
+ export function badRequestResponse (url: string, errors: Error | Error[], init?: ResponseInit): Response {
106
+ // stacktrace of the single error, or the stacktrace of the last error in the array
107
+ let stack: string | undefined
108
+ let convertedErrors: Array<{ message: string, stack: string }> | undefined
109
+ if (isArrayOfErrors(errors)) {
110
+ stack = errors[errors.length - 1].stack
111
+ convertedErrors = errors.map(e => ({ message: e.message, stack: e.stack ?? '' }))
112
+ } else if (errors instanceof Error) {
113
+ stack = errors.stack
114
+ convertedErrors = [{ message: errors.message, stack: errors.stack ?? '' }]
107
115
  }
108
- const response = new Response(body, {
109
- ...(init ?? {}),
116
+
117
+ const bodyJson = JSON.stringify({
118
+ stack,
119
+ errors: convertedErrors
120
+ })
121
+
122
+ const response = new Response(bodyJson, {
110
123
  status: 400,
111
- statusText: 'Bad Request'
124
+ statusText: 'Bad Request',
125
+ ...(init ?? {}),
126
+ headers: {
127
+ ...(init?.headers ?? {}),
128
+ 'Content-Type': 'application/json'
129
+ }
112
130
  })
113
131
 
114
132
  setType(response, 'basic')