@7365admin1/layer-common 4.2.2 → 4.2.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @iservice365/layer-common
2
2
 
3
+ ## 4.2.3
4
+
5
+ ### Patch Changes
6
+
7
+ - b2d7f77: Render a plain sentence when a request fails without reaching the API.
8
+
9
+ `errorConverter` — the app-wide error mapper used by 29 call sites in this layer — only understood a JSON API error. A gateway 502, a 503/504, an nginx or Cloudflare HTML error page, a timeout or a dropped connection fell through it, so `ofetch`'s own developer string reached the screen: `[PUT] "/api/vehicles/6a9691f25648d7633171c9e3": 502`. It now maps those to a sentence chosen from the status, never renders HTML, a JSON dump or a raw object, and still returns our own API's message unchanged whenever there is one.
10
+
11
+ The vehicle dialogs had their own local copy of that logic (`readServerMessage`), which fell back to `error.message` and was where the leak was seen. It is gone; delete, restore, update-type, approve and the add/edit form all use the shared mapper.
12
+
3
13
  ## 4.2.2
4
14
 
5
15
  ### Patch Changes
@@ -458,11 +468,11 @@
458
468
  `utils/console-tier.ts` + `composables/useConsoleTier.ts` mirror the server's
459
469
  own rule from two endpoints the console already calls, unprojected:
460
470
 
461
- GET /api/members/user/:user/app/admin the Seven365 staff membership
462
- GET /api/roles/id/:role that membership's role document
471
+ GET /api/members/user/:user/app/admin the Seven365 staff membership
472
+ GET /api/roles/id/:role that membership's role document
463
473
 
464
- owner = member.type === "admin" && role.type === "admin" && role.default === true
465
- staff = member.type === "admin" && role.type === "admin"
474
+ owner = member.type === "admin" && role.type === "admin" && role.default === true
475
+ staff = member.type === "admin" && role.type === "admin"
466
476
 
467
477
  `role.default` is the marker because it is the only property of a platform
468
478
  staff role no API caller can set - `role.controller.ts` validates create and
@@ -411,6 +411,7 @@
411
411
  </template>
412
412
 
413
413
  <script setup lang="ts">
414
+ import { errorConverter } from "../utils/data";
414
415
  const prop = defineProps({
415
416
  type: {
416
417
  type: String as PropType<TVehicleType | "seasonpass">,
@@ -865,12 +866,11 @@ async function submit() {
865
866
  // A refused create is fail-closed on the server and its message names the
866
867
  // camera that would not take the plate. `$fetch` puts it on `data`, `ofetch`
867
868
  // on `response._data`; reading only one of them dropped the reason.
868
- errorMessage.value =
869
- error?.data?.message ||
870
- error?.response?._data?.message ||
871
- `Failed to ${
872
- prop.mode === "add" ? "add" : "update"
873
- } vehicle. Please try again.`;
869
+ // `errorConverter` reads both shapes and, when the failure never reached
870
+ // the API at all (a gateway 502 answers with HTML, a dropped connection
871
+ // with nothing), returns a plain sentence instead of ofetch's
872
+ // `[POST] "/api/vehicles": 502`.
873
+ errorMessage.value = errorConverter(error);
874
874
  } finally {
875
875
  processing.value = false;
876
876
  }
@@ -210,6 +210,11 @@ import {
210
210
  vehiclePlateActions,
211
211
  type VehicleActionKey,
212
212
  } from '../utils/vehicle-actions';
213
+ // The app-wide error mapper. A gateway 502 answers with an HTML page, not a
214
+ // JSON API error, so reading `_data.message` and then falling back to
215
+ // `error.message` put ofetch's own developer string in the delete dialog:
216
+ // `[PUT] "/api/vehicles/6a9691f25648d7633171c9e3": 502`.
217
+ import { errorConverter } from '../utils/data';
213
218
 
214
219
 
215
220
 
@@ -289,21 +294,6 @@ function closeActionDialog(key: "approveVehicle" | "restoreVehicle" | "updateVeh
289
294
  actionError.value = "";
290
295
  }
291
296
 
292
- /**
293
- * The server's own sentence, or a plain fallback.
294
- *
295
- * `error.response._data` is undefined on a network failure, so reading it
296
- * unguarded threw out of the catch and the operator saw nothing at all.
297
- */
298
- function readServerMessage(error: any, fallback: string) {
299
- return (
300
- error?.response?._data?.message ||
301
- error?.data?.message ||
302
- error?.message ||
303
- fallback
304
- );
305
- }
306
-
307
297
  function showMessage(msg: string, color: string) {
308
298
  message.value = msg;
309
299
  messageColor.value = color;
@@ -566,7 +556,7 @@ async function submitDelete() {
566
556
  // request is refused. Say so. This used to assign to `message` WITHOUT
567
557
  // raising the snackbar - and `error.response._data` throws on a network
568
558
  // error - so a refused delete showed the operator nothing at all.
569
- deleteError.value = readServerMessage(error, "Failed to delete vehicle.");
559
+ deleteError.value = errorConverter(error);
570
560
  // The dialog stays open and the row stays in the table - nothing is removed
571
561
  // optimistically, so what is on screen still matches the database.
572
562
  } finally {
@@ -595,7 +585,7 @@ async function submitRestore() {
595
585
  showMessage(res.message, "success");
596
586
  await getVehiclesRefresh();
597
587
  } catch (error: any) {
598
- actionError.value = readServerMessage(error, "Failed to restore vehicle.");
588
+ actionError.value = errorConverter(error);
599
589
  } finally {
600
590
  restoringVehicle.value = false;
601
591
  }
@@ -642,7 +632,7 @@ async function submitUpdateType() {
642
632
  showMessage(res.message, "success");
643
633
  await getVehiclesRefresh();
644
634
  } catch (error: any) {
645
- actionError.value = readServerMessage(error, "Failed to update vehicle type.");
635
+ actionError.value = errorConverter(error);
646
636
  } finally {
647
637
  updatingType.value = false;
648
638
  }
@@ -667,7 +657,7 @@ async function submitApprove() {
667
657
  showMessage(res.message, "success");
668
658
  await getVehiclesRefresh();
669
659
  } catch (error: any) {
670
- actionError.value = readServerMessage(error, "Failed to approve vehicle.");
660
+ actionError.value = errorConverter(error);
671
661
  } finally {
672
662
  approvingVehicle.value = false;
673
663
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.2.2",
5
+ "version": "4.2.3",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
package/utils/data.ts CHANGED
@@ -1,33 +1,113 @@
1
+ /**
2
+ * The app-wide "turn a failed request into a sentence" helper. 29 call sites in
3
+ * this layer alone, so every module gets whatever this returns.
4
+ *
5
+ * It only ever understood a JSON API error - `{ message: "..." }` from our own
6
+ * Express error handler. Anything else fell through to a developer string. A
7
+ * gateway 502 put this in front of an operator, verbatim, inside the delete
8
+ * dialog:
9
+ *
10
+ * [PUT] "/api/vehicles/6a9691f25648d7633171c9e3": 502
11
+ *
12
+ * That is ofetch's own `error.message`. It names an internal route and a record
13
+ * id, and tells the person nothing they can do. nginx and Cloudflare answer
14
+ * with HTML, not JSON, so `_data` has no `.message` at all - and on a dropped
15
+ * connection there is no `response` either.
16
+ *
17
+ * The rules below, in order:
18
+ * 1. our own API said something -> say exactly that, unchanged. This is the
19
+ * path every working screen already takes and it must not move.
20
+ * 2. otherwise -> a plain sentence chosen from the status.
21
+ * 3. never -> HTML, a JSON dump, an ofetch `[METHOD] "url": status` string,
22
+ * or a raw object.
23
+ */
24
+
25
+ /** ofetch builds `[GET] "/api/thing": 502 Bad Gateway`. Never show it. */
26
+ const OFETCH_MESSAGE = /^\[[A-Z]+\]\s+"/;
27
+
28
+ /** A body that is a document, not an API error. */
29
+ const LOOKS_LIKE_HTML = /^\s*(<!doctype|<html|<)/i;
30
+
31
+ const GENERIC =
32
+ "Something went wrong. Please try again or contact support if the problem persists.";
33
+
34
+ /**
35
+ * The server's own sentence, if there is one that is safe to show.
36
+ *
37
+ * Returns `""` when the body is not one of ours - an HTML error page, a JSON
38
+ * object with no `message`, or an empty response.
39
+ */
40
+ function apiMessage(error: any): string {
41
+ const data = error?.response?._data ?? error?.data ?? error?._data;
42
+
43
+ const candidate =
44
+ typeof data === "string" ? data : (data?.message ?? data?.error);
45
+
46
+ if (typeof candidate !== "string") return "";
47
+
48
+ const text = candidate.trim();
49
+ if (!text) return "";
50
+ if (LOOKS_LIKE_HTML.test(text)) return "";
51
+ // A whole HTML page that starts with whitespace/JSON-ish noise still has no
52
+ // business on screen; anything this long is a document, not a message.
53
+ if (text.length > 400) return "";
54
+
55
+ return text;
56
+ }
57
+
58
+ /** A plain sentence for a request that failed without telling us anything. */
59
+ function sentenceForStatus(status: number | null): string {
60
+ if (status === null) {
61
+ // No response at all: DNS, a dropped connection, a cancelled request.
62
+ return "Could not reach the server. Check your internet connection and try again.";
63
+ }
64
+ if (status === 401) return "Your session has expired. Please sign in again.";
65
+ if (status === 403) {
66
+ return "You do not have permission to do this. Ask your iService365 administrator for access.";
67
+ }
68
+ if (status === 404) return "That record no longer exists. Refresh the page and try again.";
69
+ if (status === 408 || status === 504) {
70
+ return "The server took too long to answer. Please try again in a few moments.";
71
+ }
72
+ if (status === 429) return "Too many attempts in a short time. Please wait a moment and try again.";
73
+ if (status === 502 || status === 503) {
74
+ return "The server is temporarily unavailable. Please try again in a few moments.";
75
+ }
76
+ if (status >= 500) {
77
+ return "The server could not complete this request. Please try again, and contact support if it keeps happening.";
78
+ }
79
+ return GENERIC;
80
+ }
81
+
1
82
  export const errorConverter = (data: any): string => {
2
- let error =
3
- "Something went wrong. Please try again or contact support if the problem persists.";
4
-
5
- if (data?.response) {
6
- console.log("errorConverter response: ", data.response);
7
- if (
8
- data.response.status == 400 ||
9
- data.response.status == 500 ||
10
- data.response.status == 502 ||
11
- (data.response.status == 404 && data.response._data.message)
12
- ) {
13
- if (data.response._data) {
14
- return (error = data.response._data.message || data.response._data);
15
- }
16
- } else if (data.response._data) {
17
- return (error = data.response._data.message || data.response._data);
18
- } else if (data.response.status == 404) {
19
- return "Server Endpoint Not Found";
20
- } else if (data.response.status == 502) {
21
- return "The server is temporarily unavailable. Please try again in a few moments.";
22
- }
23
- error = `Error ${data.response.status}: ${JSON.stringify(
24
- data.response._data
25
- )}`;
26
- } else if (data?.message) {
27
- console.log("converter: ", data);
28
- error = data?.message;
83
+ // 1. Our own API's sentence wins, whatever the status is. Unchanged
84
+ // behaviour for every screen that works today.
85
+ const fromApi = apiMessage(data);
86
+ if (fromApi) return fromApi;
87
+
88
+ // 2. No usable body. Decide from the status alone.
89
+ const status =
90
+ data?.response?.status ?? data?.statusCode ?? data?.status ?? null;
91
+
92
+ if (typeof status === "number") return sentenceForStatus(status);
93
+
94
+ // 3. No status either. An ofetch error with no response means the request
95
+ // never completed at all - DNS, a dropped connection, a cancelled
96
+ // request - and its `message` is the developer string that leaked.
97
+ const message = typeof data?.message === "string" ? data.message.trim() : "";
98
+
99
+ if (OFETCH_MESSAGE.test(message) || data?.name === "FetchError") {
100
+ return sentenceForStatus(null);
101
+ }
102
+
103
+ // 4. A plain `throw new Error("...")` from our own code. Several of those are
104
+ // written FOR the toast (see `findServiceProviderIdBySite` below), so they
105
+ // still pass through unchanged.
106
+ if (message && message.length <= 400 && !LOOKS_LIKE_HTML.test(message)) {
107
+ return message;
29
108
  }
30
- return error;
109
+
110
+ return GENERIC;
31
111
  };
32
112
 
33
113
  /**