@beyonk/http 12.4.2 → 12.5.0

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.
package/README.MD CHANGED
@@ -192,6 +192,8 @@ Handlers have a signature with two items:
192
192
  })
193
193
  ```
194
194
 
195
+ Every error carries the request that failed as `e.request` (`{ method, url }`), and HTTP failures also carry `e.status` and the parsed response as `e.body`. Errors thrown by `fetch` itself, such as network failures or aborts, have `e.request` but no `e.status`.
196
+
195
197
  ctx can be whatever you want really - it is whatever you pass in as `context(...)`.
196
198
 
197
199
  However, if the `context` object you pass in has a `fetch` function, this is used as the `fetch` for XHR requests.
package/dist/index.cjs CHANGED
@@ -29,10 +29,13 @@ var HttpError = class extends Error {
29
29
  * Create a new HTTP error
30
30
  * @param {string} message - Error message
31
31
  * @param {any} body - Error response body
32
+ * @param {number} [status] - HTTP status code, absent when the request never got a response
32
33
  */
33
- constructor(message, body) {
34
+ constructor(message, body, status) {
34
35
  super(message);
35
36
  this.body = body;
37
+ this.status = status;
38
+ this.request = void 0;
36
39
  }
37
40
  };
38
41
  var AccessDeniedError = class extends HttpError {
@@ -175,7 +178,7 @@ var Api = class {
175
178
  try {
176
179
  result = await this.#doQuery(1, client, ep, options);
177
180
  } catch (e) {
178
- console.log(e);
181
+ e.request = { method: options.method, url: ep };
179
182
  return this.handle(e, this.ctx);
180
183
  } finally {
181
184
  this.resetRequest();
@@ -228,7 +231,7 @@ var Api = class {
228
231
  console.log("Failed to parse error body when asked.");
229
232
  }
230
233
  const ClientError = getErrorByCode(r.status);
231
- throw new ClientError(r.statusText, content);
234
+ throw new ClientError(r.statusText, content, r.status);
232
235
  } catch (e) {
233
236
  if (retry.attempts && retry.errors && attempt < retry.attempts && retry.errors.includes(e.code)) {
234
237
  console.warn(`Got ${e.code} when calling ${endpoint}. Retrying request (${attempt}/${retry.attempts})`);
package/dist/index.d.cts CHANGED
@@ -98,9 +98,16 @@ declare class HttpError extends Error {
98
98
  * Create a new HTTP error
99
99
  * @param {string} message - Error message
100
100
  * @param {any} body - Error response body
101
+ * @param {number} [status] - HTTP status code, absent when the request never got a response
101
102
  */
102
- constructor(message: string, body: any);
103
+ constructor(message: string, body: any, status?: number);
103
104
  body: any;
105
+ status: number;
106
+ /** @type {{ method: string, url: string } | undefined} The request that failed */
107
+ request: {
108
+ method: string;
109
+ url: string;
110
+ } | undefined;
104
111
  }
105
112
  declare namespace _default {
106
113
  export { create };
package/dist/index.d.ts CHANGED
@@ -98,9 +98,16 @@ declare class HttpError extends Error {
98
98
  * Create a new HTTP error
99
99
  * @param {string} message - Error message
100
100
  * @param {any} body - Error response body
101
+ * @param {number} [status] - HTTP status code, absent when the request never got a response
101
102
  */
102
- constructor(message: string, body: any);
103
+ constructor(message: string, body: any, status?: number);
103
104
  body: any;
105
+ status: number;
106
+ /** @type {{ method: string, url: string } | undefined} The request that failed */
107
+ request: {
108
+ method: string;
109
+ url: string;
110
+ } | undefined;
104
111
  }
105
112
  declare namespace _default {
106
113
  export { create };
package/dist/index.js CHANGED
@@ -4,10 +4,13 @@ var HttpError = class extends Error {
4
4
  * Create a new HTTP error
5
5
  * @param {string} message - Error message
6
6
  * @param {any} body - Error response body
7
+ * @param {number} [status] - HTTP status code, absent when the request never got a response
7
8
  */
8
- constructor(message, body) {
9
+ constructor(message, body, status) {
9
10
  super(message);
10
11
  this.body = body;
12
+ this.status = status;
13
+ this.request = void 0;
11
14
  }
12
15
  };
13
16
  var AccessDeniedError = class extends HttpError {
@@ -150,7 +153,7 @@ var Api = class {
150
153
  try {
151
154
  result = await this.#doQuery(1, client, ep, options);
152
155
  } catch (e) {
153
- console.log(e);
156
+ e.request = { method: options.method, url: ep };
154
157
  return this.handle(e, this.ctx);
155
158
  } finally {
156
159
  this.resetRequest();
@@ -203,7 +206,7 @@ var Api = class {
203
206
  console.log("Failed to parse error body when asked.");
204
207
  }
205
208
  const ClientError = getErrorByCode(r.status);
206
- throw new ClientError(r.statusText, content);
209
+ throw new ClientError(r.statusText, content, r.status);
207
210
  } catch (e) {
208
211
  if (retry.attempts && retry.errors && attempt < retry.attempts && retry.errors.includes(e.code)) {
209
212
  console.warn(`Got ${e.code} when calling ${endpoint}. Retrying request (${attempt}/${retry.attempts})`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@beyonk/http",
3
- "version": "12.4.2",
3
+ "version": "12.5.0",
4
4
  "description": "An isomorphic http client for Svelte apps",
5
5
  "type": "module",
6
6
  "repository": "https://github.com/beyonk/http.git",