@xoong/data 0.0.39 → 0.0.42

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/client.js +7 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xoong/data",
3
- "version": "0.0.39",
3
+ "version": "0.0.42",
4
4
  "description": "xoong data layer: OpenAPI-driven client, auto cache, entity store, derived views, live updates. Depends only on @xoong/core.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -15,7 +15,7 @@
15
15
  "sideEffects": false,
16
16
  "license": "MIT",
17
17
  "dependencies": {
18
- "@xoong/core": "0.0.39"
18
+ "@xoong/core": "0.0.42"
19
19
  },
20
20
  "files": [
21
21
  "src"
@@ -35,7 +35,7 @@
35
35
  "bugs": {
36
36
  "url": "https://github.com/fpt-duongtx/xoong/issues"
37
37
  },
38
- "homepage": "https://github.com/fpt-duongtx/xoong#readme",
38
+ "homepage": "https://fpt-duongtx.github.io/xoong/",
39
39
  "publishConfig": {
40
40
  "access": "public"
41
41
  },
package/src/client.js CHANGED
@@ -160,7 +160,8 @@ export function createClient(options) {
160
160
  if (res.ok) {
161
161
  if (res.status === 204) return null;
162
162
  const text = await res.text();
163
- const json = text ? JSON.parse(text) : null;
163
+ let json;
164
+ try { json = text ? JSON.parse(text) : null; } catch (cause) { throw new XoongError({ status: res.status, operation: o.id, cause, detail: 'the response is not valid JSON' }); }
164
165
  if (o.unwrap && json && typeof json === 'object') { // { code, message, data } envelopes
165
166
  if (!(o.unwrap in json)) throw new XoongError({ status: res.status, operation: o.id, detail: `the response has no "${o.unwrap}" field (hint "unwrap" expects an envelope)` });
166
167
  return json[o.unwrap] ?? null;
@@ -333,9 +334,11 @@ export function createClient(options) {
333
334
  json = await request(o, params, withKeys(o, body));
334
335
  } catch (err) {
335
336
  undo.reverse().forEach((u) => u());
336
- const e = err instanceof XoongError ? err : new XoongError({ operation: o.id, cause: err });
337
- fail(e);
338
- throw e;
337
+ // A failed request is already a XoongError (a fetch failure is "Network error", a bad status carries the server's answer). Anything
338
+ // else is a mistake in the caller's code (a `headers()` callback that throws, a body that cannot be serialised): pass it on as it is.
339
+ if (!(err instanceof XoongError)) throw err;
340
+ fail(err);
341
+ throw err;
339
342
  } finally {
340
343
  mutating--;
341
344
  }