|
1 | | -export interface SettingsLoadError { |
| 1 | +import { apiFailure } from "../api/api-failure.js"; |
| 2 | + |
| 3 | +export interface SettingsError { |
2 | 4 | headline: string; |
3 | 5 | message: string; |
4 | 6 | } |
5 | 7 |
|
6 | | -export type SettingsAction = "save" | "test"; |
| 8 | +export type SettingsOperation = "load" | "save" | "test"; |
7 | 9 |
|
8 | | -export function settingsLoadError(error: unknown, responseStatus?: number): SettingsLoadError { |
9 | | - const status = requestStatus(error, responseStatus); |
| 10 | +export function settingsError(operation: SettingsOperation, error: unknown, responseStatus?: number): SettingsError { |
| 11 | + const failure = apiFailure(error, responseStatus); |
| 12 | + const retryAction = operation === "load" ? "retry loading Web Analytics settings" : operation === "save" ? "save your preserved changes" : "retry the connection test"; |
| 13 | + const subject = operation === "test" ? "The connection test" : "Web Analytics settings"; |
10 | 14 |
|
11 | | - switch (status) { |
12 | | - case 401: |
13 | | - return { |
14 | | - headline: "Sign in again", |
15 | | - message: "Your backoffice session has expired. Sign in again, then retry loading Web Analytics settings.", |
16 | | - }; |
17 | | - case 403: |
18 | | - return { |
19 | | - headline: "Administrator access required", |
20 | | - message: "Only administrators can manage Web Analytics settings. Ask an administrator to open this page.", |
21 | | - }; |
22 | | - case 404: |
23 | | - return { |
24 | | - headline: "Package files are out of sync", |
25 | | - message: "The settings API was not found. Restart Umbraco after updating Web Analytics, then refresh the backoffice so the server and package files use the same version.", |
26 | | - }; |
27 | | - case 429: |
28 | | - return { |
29 | | - headline: "Too many requests", |
30 | | - message: "Web Analytics settings cannot be loaded right now. Wait a moment, then retry.", |
31 | | - }; |
32 | | - default: |
33 | | - if (status !== undefined && status >= 500) { |
34 | | - return { |
35 | | - headline: "Settings service unavailable", |
36 | | - message: "Umbraco could not load Web Analytics settings. Retry, and check the Umbraco logs if the problem continues.", |
37 | | - }; |
38 | | - } |
39 | | - return { |
40 | | - headline: "Could not reach the settings service", |
41 | | - message: "Check your connection and that Umbraco is running, then retry loading Web Analytics settings.", |
42 | | - }; |
43 | | - } |
44 | | -} |
45 | | - |
46 | | -export function settingsActionError(action: SettingsAction, error: unknown, responseStatus?: number): string { |
47 | | - const status = requestStatus(error, responseStatus); |
48 | | - const subject = action === "save" ? "Web Analytics settings" : "The connection test"; |
49 | | - |
50 | | - switch (status) { |
| 15 | + switch (failure.status) { |
51 | 16 | case 400: |
52 | | - return action === "save" |
53 | | - ? problemDetail(error) ?? "Settings were not saved. Review the connection fields and mapping values." |
54 | | - : "The connection test request was invalid. Check the saved connection configuration."; |
| 17 | + return operation === "save" |
| 18 | + ? { headline: "Review the settings", message: failure.detail ?? "Settings were not saved. Review the connection fields and mapping values." } |
| 19 | + : operation === "test" |
| 20 | + ? { headline: "Invalid connection test", message: "The connection test request was invalid. Check the saved connection configuration." } |
| 21 | + : unavailable("Settings request rejected", "Umbraco rejected the settings request. Refresh the backoffice, then retry."); |
55 | 22 | case 401: |
56 | | - return `Your backoffice session has expired. Sign in again, then ${action === "save" ? "save your preserved changes" : "retry the connection test"}.`; |
| 23 | + return unavailable("Sign in again", `Your backoffice session has expired. Sign in again, then ${retryAction}.`); |
57 | 24 | case 403: |
58 | | - return `Administrator access is required to ${action === "save" ? "save Web Analytics settings" : "test analytics connections"}.`; |
| 25 | + return unavailable("Administrator access required", operation === "load" |
| 26 | + ? "Only administrators can manage Web Analytics settings. Ask an administrator to open this page." |
| 27 | + : `Administrator access is required to ${operation === "save" ? "save Web Analytics settings" : "test analytics connections"}.`); |
59 | 28 | case 404: |
60 | | - return `${subject} API was not found. Restart Umbraco after updating Web Analytics, then refresh the backoffice so the server and package files use the same version.`; |
| 29 | + return unavailable("Package files are out of sync", `${operation === "load" ? "The settings" : subject} API was not found. Restart Umbraco after updating Web Analytics, then refresh the backoffice so the server and package files use the same version.`); |
61 | 30 | case 429: |
62 | | - return `${subject} could not be completed because there were too many requests. Wait a moment, then retry.`; |
| 31 | + return unavailable("Too many requests", `${subject} ${operation === "load" ? "cannot be loaded" : "could not be completed"} right now. Wait a moment, then retry.`); |
63 | 32 | default: |
64 | | - if (status !== undefined && status >= 500) { |
65 | | - return `${subject} could not be completed. Retry, and check the Umbraco logs if the problem continues.`; |
| 33 | + if (failure.status !== undefined && failure.status >= 500) { |
| 34 | + return unavailable("Settings service unavailable", `${subject} could not be ${operation === "load" ? "loaded" : "completed"}. Retry, and check the Umbraco logs if the problem continues.`); |
66 | 35 | } |
67 | | - return `${subject} could not reach Umbraco. Check your connection and that Umbraco is running, then retry.`; |
| 36 | + return operation === "load" |
| 37 | + ? unavailable("Could not reach the settings service", "Check your connection and that Umbraco is running, then retry loading Web Analytics settings.") |
| 38 | + : unavailable("Could not reach the settings service", `${subject} could not reach Umbraco. Check your connection and that Umbraco is running, then retry.`); |
68 | 39 | } |
69 | 40 | } |
70 | 41 |
|
71 | | -function requestStatus(error: unknown, responseStatus?: number): number | undefined { |
72 | | - const errorStatus = typeof error === "object" && error !== null && "status" in error |
73 | | - ? Number(error.status) |
74 | | - : undefined; |
75 | | - return responseStatus || errorStatus; |
76 | | -} |
77 | | - |
78 | | -function problemDetail(error: unknown): string | undefined { |
79 | | - if (typeof error !== "object" || error === null || !("detail" in error)) return undefined; |
80 | | - return typeof error.detail === "string" && error.detail.trim() ? error.detail : undefined; |
| 42 | +function unavailable(headline: string, message: string): SettingsError { |
| 43 | + return { headline, message }; |
81 | 44 | } |
0 commit comments