@geode/opengeodeweb-front 12.1.0-rc.6 → 12.1.0-rc.7

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.
@@ -2,6 +2,8 @@ import { v4 as uuidv4 } from "uuid";
2
2
 
3
3
  const MILLISECONDS_IN_SECOND = 1000;
4
4
  const DEFAULT_FEEDBACKS_TIMEOUT_SECONDS = 10;
5
+ const DEFAULT_ERROR_NAME = "Error";
6
+ const DEFAULT_ERROR_DESCRIPTION = "An unexpected error occurred";
5
7
 
6
8
  type FeedbackType = "error" | "success" | "warning";
7
9
 
@@ -34,8 +36,9 @@ export const useFeedbackStore = defineStore("feedback", {
34
36
  type: "error",
35
37
  code,
36
38
  route,
37
- name,
38
- description,
39
+ name: name === "" ? DEFAULT_ERROR_NAME : name,
40
+ // The snackbar only shows the description: never leave it empty
41
+ description: [description, name].find((text) => text !== "") ?? DEFAULT_ERROR_DESCRIPTION,
39
42
  });
40
43
  setTimeout(() => {
41
44
  this.delete_feedback(feedbackId);
@@ -1,6 +1,6 @@
1
1
  import type { JsonRpcSchema, RequestHandlersWithValidation } from "@ogw_shared/utils/types.js";
2
2
  import { endRequestLog, startRequestLog } from "@ogw_front/utils/log";
3
- import type { ErrorResponse } from "@geode/opengeodeweb-back/opengeodeweb_back_typed_schemas.js";
3
+ import { reportError, reportTimeout } from "./error_feedback.js";
4
4
  import { consola } from "consola";
5
5
  import { fetchSchema } from "@ogw_shared/utils/fetch_schema";
6
6
  import { useFeedbackStore } from "@ogw_front/stores/feedback";
@@ -20,45 +20,6 @@ interface ApiFetchParams {
20
20
  headers?: Record<string, string>;
21
21
  }
22
22
 
23
- // Loosely-typed shapes for the dynamic error/response values reported by fetchSchema.
24
- interface FetchErrorLike {
25
- code?: number;
26
- message?: string;
27
- stack?: string;
28
- }
29
-
30
- type FetchErrorResponseLike = { status?: number } & Partial<
31
- Pick<ErrorResponse, "name" | "description">
32
- >;
33
-
34
- function isRecord(value: unknown): value is Record<string, unknown> {
35
- return typeof value === "object" && value !== null;
36
- }
37
-
38
- function toFetchErrorLike(error: unknown): FetchErrorLike {
39
- if (!isRecord(error)) {
40
- return {};
41
- }
42
- return {
43
- code: typeof error.code === "number" ? error.code : undefined,
44
- message: typeof error.message === "string" ? error.message : undefined,
45
- stack: typeof error.stack === "string" ? error.stack : undefined,
46
- };
47
- }
48
-
49
- function toFetchErrorResponseLike(response: unknown): FetchErrorResponseLike {
50
- if (!isRecord(response)) {
51
- return {};
52
- }
53
- // Ofetch puts the parsed body (an ErrorResponse for Flask microservices) in `_data`
54
- const body = isRecord(response._data) ? response._data : response;
55
- return {
56
- status: typeof response.status === "number" ? response.status : undefined,
57
- name: typeof body.name === "string" ? body.name : undefined,
58
- description: typeof body.description === "string" ? body.description : undefined,
59
- };
60
- }
61
-
62
23
  // oxlint-disable-next-line max-lines-per-function
63
24
  async function api_fetch(
64
25
  microservice: Microservice,
@@ -76,7 +37,7 @@ async function api_fetch(
76
37
  microservice.start_request();
77
38
 
78
39
  const requestStartingTime = startRequestLog(microservice, schema);
79
- const result = await fetchSchema(
40
+ const request = fetchSchema(
80
41
  {
81
42
  schema,
82
43
  params,
@@ -87,14 +48,8 @@ async function api_fetch(
87
48
  {
88
49
  async request_error_function(error: unknown) {
89
50
  microservice.stop_request();
90
- const typedError = toFetchErrorLike(error);
91
51
  if (skip_feedback_error !== true) {
92
- feedbackStore.add_error(
93
- typedError.code ?? 0,
94
- schema.$id,
95
- typedError.message ?? "",
96
- typedError.stack ?? "",
97
- );
52
+ reportError(schema.$id, error);
98
53
  }
99
54
  if (request_error_function) {
100
55
  await request_error_function(error);
@@ -109,14 +64,8 @@ async function api_fetch(
109
64
  },
110
65
  async response_error_function(response: unknown) {
111
66
  microservice.stop_request();
112
- const typedResponse = toFetchErrorResponseLike(response);
113
67
  if (skip_feedback_error !== true) {
114
- feedbackStore.add_error(
115
- typedResponse.status ?? 0,
116
- schema.$id,
117
- typedResponse.name ?? "",
118
- typedResponse.description ?? "",
119
- );
68
+ reportError(schema.$id, response);
120
69
  }
121
70
  if (response_error_function) {
122
71
  await response_error_function(response);
@@ -128,8 +77,12 @@ async function api_fetch(
128
77
  },
129
78
  },
130
79
  );
80
+ if (skip_feedback_error === true) {
81
+ return request;
82
+ }
83
+ const result = await reportTimeout(schema.$id, request);
131
84
  return result;
132
85
  }
133
86
 
134
87
  export { api_fetch };
135
- export type { FetchErrorLike, Microservice };
88
+ export type { Microservice };
@@ -0,0 +1,106 @@
1
+ import type { ErrorResponse } from "@geode/opengeodeweb-back/opengeodeweb_back_typed_schemas.js";
2
+ import { TimeoutError } from "p-timeout";
3
+ import { useFeedbackStore } from "@ogw_front/stores/feedback";
4
+
5
+ const UNKNOWN_CODE = 0;
6
+ const DEFAULT_NAME = "Error";
7
+ const DEFAULT_DESCRIPTION = "An unexpected error occurred";
8
+
9
+ // Wslink sends a Python exception as its repr, e.g. ValueError('No BRep object found')
10
+ const PYTHON_REPR = /^(?<name>[A-Za-z_][\w.]*)\((?<args>.*)\)$/su;
11
+ const QUOTED_STRING = /^(?<quote>['"])(?<content>.*)\k<quote>$/su;
12
+
13
+ type Fields = Record<string, unknown>;
14
+
15
+ function isRecord(value: unknown): value is Fields {
16
+ return typeof value === "object" && value !== null;
17
+ }
18
+
19
+ function stringField(record: Fields, key: string): string | undefined {
20
+ const value = record[key];
21
+ return typeof value === "string" && value !== "" ? value : undefined;
22
+ }
23
+
24
+ function numberField(record: Fields, key: string): number | undefined {
25
+ const value = record[key];
26
+ return typeof value === "number" ? value : undefined;
27
+ }
28
+
29
+ function parsePythonException(repr: string | undefined): Partial<ErrorResponse> {
30
+ if (repr === undefined) {
31
+ return {};
32
+ }
33
+ const match = PYTHON_REPR.exec(repr.trim());
34
+ if (!match?.groups) {
35
+ return { description: repr };
36
+ }
37
+ const { name } = match.groups;
38
+ const args = match.groups.args?.trim() ?? "";
39
+ const quoted = QUOTED_STRING.exec(args);
40
+ return { name, description: quoted?.groups?.content ?? (args === "" ? undefined : args) };
41
+ }
42
+
43
+ /*
44
+ * Turn whatever a microservice call failed with into one { code, name, description }:
45
+ * - an ofetch response, whose parsed body (`_data`) is an ErrorResponse (Flask back, Nuxt server routes)
46
+ * - an ErrorResponse body itself
47
+ * - a wslink error { code, message, data: { exception, trace } } (viewer)
48
+ * - any thrown Error (network failure, timeout...)
49
+ */
50
+ function toErrorFeedback(value: unknown): ErrorResponse {
51
+ if (!isRecord(value)) {
52
+ return {
53
+ code: UNKNOWN_CODE,
54
+ name: DEFAULT_NAME,
55
+ description: typeof value === "string" && value !== "" ? value : DEFAULT_DESCRIPTION,
56
+ };
57
+ }
58
+ // Ofetch puts the parsed response body in `_data`
59
+ let body = value;
60
+ if (isRecord(value._data)) {
61
+ body = value._data;
62
+ }
63
+ let exception: Partial<ErrorResponse> = {};
64
+ if (isRecord(body.data)) {
65
+ exception = parsePythonException(stringField(body.data, "exception"));
66
+ }
67
+ return {
68
+ code:
69
+ numberField(value, "status") ??
70
+ numberField(body, "code") ??
71
+ numberField(value, "statusCode") ??
72
+ UNKNOWN_CODE,
73
+ name:
74
+ stringField(body, "name") ??
75
+ exception.name ??
76
+ stringField(value, "statusText") ??
77
+ stringField(value, "name") ??
78
+ DEFAULT_NAME,
79
+ description:
80
+ stringField(body, "description") ??
81
+ exception.description ??
82
+ stringField(body, "message") ??
83
+ stringField(value, "message") ??
84
+ DEFAULT_DESCRIPTION,
85
+ };
86
+ }
87
+
88
+ // The single entry point to show a microservice error to the user
89
+ function reportError(route: string, value: unknown): void {
90
+ const { code, name, description } = toErrorFeedback(value);
91
+ useFeedbackStore().add_error(code, route, name, description);
92
+ }
93
+
94
+ // A timeout rejects without going through the request handlers: report it as well
95
+ async function reportTimeout<Result>(route: string, request: Promise<Result>): Promise<Result> {
96
+ try {
97
+ return await request;
98
+ } catch (error) {
99
+ if (error instanceof TimeoutError) {
100
+ reportError(route, error);
101
+ }
102
+ throw error;
103
+ }
104
+ }
105
+
106
+ export { reportError, reportTimeout, toErrorFeedback };
@@ -1,10 +1,10 @@
1
- import type { FetchErrorLike, Microservice } from "./api_fetch.js";
2
1
  import type { JsonRpcSchema, RequestHandlers } from "@ogw_shared/utils/types.js";
3
2
  import { CHUNK_SIZE_BYTES } from "@ogw_shared/utils/file.js";
3
+ import type { Microservice } from "./api_fetch.js";
4
4
  import { consola } from "consola";
5
5
  import { fetchRaw } from "@ogw_shared/utils/fetch_raw.js";
6
+ import { reportError } from "./error_feedback.js";
6
7
  import { uploadPath } from "@ogw_front/utils/upload_path";
7
- import { useFeedbackStore } from "@ogw_front/stores/feedback.js";
8
8
 
9
9
  interface UploadFileParams {
10
10
  // Always forwarded to fetchRaw via schema.methods, which is only ever set on
@@ -14,28 +14,12 @@ interface UploadFileParams {
14
14
  params?: Record<string, string>;
15
15
  }
16
16
 
17
- // Loosely-typed shapes for the dynamic error/response values reported by fetchRaw.
18
- interface FetchErrorResponseLike {
19
- status?: number;
20
- name?: string;
21
- description?: string;
22
- }
23
-
24
- function isFetchErrorLike(value: unknown): value is FetchErrorLike {
25
- return typeof value === "object" && value !== null;
26
- }
27
-
28
- function isFetchErrorResponseLike(value: unknown): value is FetchErrorResponseLike {
29
- return typeof value === "object" && value !== null;
30
- }
31
-
32
17
  async function upload_file(
33
18
  microservice: Microservice,
34
19
  { schema, file, params = {} }: UploadFileParams,
35
20
  { request_error_function, response_function, response_error_function }: RequestHandlers = {},
36
21
  ): Promise<unknown> {
37
22
  consola.debug("[UPLOAD_FILE] Uploading file", { schema, file });
38
- const feedbackStore = useFeedbackStore();
39
23
 
40
24
  if (!(file instanceof File)) {
41
25
  throw new TypeError("file must be an instance of File");
@@ -47,13 +31,7 @@ async function upload_file(
47
31
 
48
32
  function onRequestError(error: unknown): void {
49
33
  microservice.stop_request();
50
- const typedError = isFetchErrorLike(error) ? error : {};
51
- feedbackStore.add_error(
52
- typedError.code ?? 0,
53
- route,
54
- typedError.message ?? "",
55
- typedError.stack ?? "",
56
- );
34
+ reportError(route, error);
57
35
  if (request_error_function) {
58
36
  void request_error_function(error);
59
37
  }
@@ -61,13 +39,7 @@ async function upload_file(
61
39
 
62
40
  function onResponseError(response: unknown): void {
63
41
  microservice.stop_request();
64
- const typedResponse = isFetchErrorResponseLike(response) ? response : {};
65
- feedbackStore.add_error(
66
- typedResponse.status ?? 0,
67
- route,
68
- typedResponse.name ?? "",
69
- typedResponse.description ?? "",
70
- );
42
+ reportError(route, response);
71
43
  if (response_error_function) {
72
44
  void response_error_function(response);
73
45
  }
@@ -1,5 +1,6 @@
1
1
  import type { JsonRpcSchema, RequestHandlersWithValidation } from "@ogw_shared/utils/types.js";
2
2
  import { endRequestLog, startRequestLog } from "@ogw_front/utils/log";
3
+ import { reportError, reportTimeout } from "./error_feedback.js";
3
4
  import type { Microservice } from "./api_fetch.js";
4
5
  import type { RpcClient } from "@ogw_shared/utils/call_raw.js";
5
6
  import { callSchema } from "@ogw_shared/utils/call_schema";
@@ -15,22 +16,6 @@ interface ViewerCallParams {
15
16
  timeout?: number;
16
17
  }
17
18
 
18
- // Loosely-typed shape for the dynamic error/response values reported by callSchema.
19
- interface RpcErrorLike {
20
- code?: number;
21
- message?: string;
22
- }
23
-
24
- function toRpcErrorLike(value: unknown): RpcErrorLike {
25
- if (typeof value !== "object" || value === null) {
26
- return {};
27
- }
28
- return {
29
- code: "code" in value && typeof value.code === "number" ? value.code : undefined,
30
- message: "message" in value && typeof value.message === "string" ? value.message : undefined,
31
- };
32
- }
33
-
34
19
  async function viewer_call(
35
20
  microservice: ViewerMicroservice,
36
21
  { schema, params = {}, timeout }: ViewerCallParams,
@@ -45,7 +30,7 @@ async function viewer_call(
45
30
  microservice.start_request();
46
31
 
47
32
  const requestStartingTime = startRequestLog(microservice, schema);
48
- const result = await callSchema(
33
+ const request = callSchema(
49
34
  {
50
35
  schema,
51
36
  params,
@@ -53,37 +38,24 @@ async function viewer_call(
53
38
  timeout,
54
39
  },
55
40
  {
56
- request_error_function(error: unknown) {
57
- microservice.stop_request();
58
- const typedError = toRpcErrorLike(error);
59
- feedbackStore.add_error(
60
- typedError.code ?? 0,
61
- schema.$id,
62
- typedError.message ?? "",
63
- typedError.message ?? "",
64
- );
41
+ // CallRaw runs both error handlers on the same rejection: stop the request and report it once, in the response one.
42
+ async request_error_function(error: unknown) {
65
43
  if (request_error_function) {
66
- void request_error_function(error);
44
+ await request_error_function(error);
67
45
  }
68
46
  },
69
- response_function(data: unknown) {
47
+ async response_function(data: unknown) {
70
48
  endRequestLog(microservice, schema, requestStartingTime);
71
49
  microservice.stop_request();
72
50
  if (response_function) {
73
- response_function(data);
51
+ await response_function(data);
74
52
  }
75
53
  },
76
- response_error_function(response: unknown) {
54
+ async response_error_function(response: unknown) {
77
55
  microservice.stop_request();
78
- const typedResponse = toRpcErrorLike(response);
79
- feedbackStore.add_error(
80
- typedResponse.code ?? 0,
81
- schema.$id,
82
- typedResponse.message ?? "",
83
- typedResponse.message ?? "",
84
- );
56
+ reportError(schema.$id, response);
85
57
  if (response_error_function) {
86
- void response_error_function(response);
58
+ await response_error_function(response);
87
59
  }
88
60
  },
89
61
  validation_error_function({ code, name, error }) {
@@ -92,6 +64,7 @@ async function viewer_call(
92
64
  },
93
65
  },
94
66
  );
67
+ const result = await reportTimeout(schema.$id, request);
95
68
  return result;
96
69
  }
97
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geode/opengeodeweb-front",
3
- "version": "12.1.0-rc.6",
3
+ "version": "12.1.0-rc.7",
4
4
  "description": "OpenSource Vue/Nuxt/Pinia/Vuetify framework for web applications",
5
5
  "homepage": "https://github.com/Geode-solutions/OpenGeodeWeb-Front",
6
6
  "bugs": {