PluginProbe
Code Snippets / 4.0.0-beta.3
Code Snippets v4.0.0-beta.3
4.0.0-beta.3 4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 All 66 releases
← All changes | js/utils/errors.ts +71 -0 3.9.6 → 4.0.0-beta.3 View file →
@@ -1,3 +1,74 @@
1 +import { __ } from '@wordpress/i18n'
2 +import { isAxiosError, isCancel } from 'axios'
3 +
1 4 export const handleUnknownError = (error: unknown) => {
2 5 console.error(error)
6 +}
7 +
8 +/**
9 + * Whether a rejection came from the caller aborting the request rather than
10 + * from a failure — axios rejects cancelled requests with no response attached,
11 + * so these must not be surfaced to the user as errors.
12 + */
13 +export const isAbortError = (error: unknown): boolean =>
14 + isCancel(error) || error instanceof DOMException && 'AbortError' === error.name
15 +
16 +export const unpackErrorResponse = (error: unknown): string => {
17 + if (isAxiosError(error)) {
18 + if (error.response) {
19 + const responseData: unknown = error.response.data
20 +
21 + if (responseData && 'object' === typeof responseData && 'message' in responseData) {
22 + return String(responseData.message)
23 + }
24 + }
25 +
26 + return error.message
27 + }
28 +
29 + return __('An unknown error occurred.', 'code-snippets')
30 +}
31 +
32 +/**
33 + * Whether a request finished without the browser receiving a response.
34 + *
35 + * Nothing can be concluded about the write itself from this: the request may
36 + * never have arrived, or it may have been handled and the response lost on the
37 + * way back. Aborts are excluded, as the caller stopped those deliberately.
38 + */
39 +export const isUnconfirmedRequest = (error: unknown): boolean =>
40 + isAxiosError(error) && !error.response && !isAbortError(error)
41 +
42 +/**
43 + * Explain a failed request in terms the reader can act on.
44 + *
45 + * An expired session is the common case worth naming: the snippet editor is a
46 + * screen people leave open, and once the session lapses WordPress rejects every
47 + * write with a 403 that says only "Cookie check failed". Reporting the raw
48 + * status left people believing the plugin had ignored them.
49 + */
50 +export const describeRequestError = (error: unknown): string => {
51 + if (!isAxiosError(error)) {
52 + return unpackErrorResponse(error)
53 + }
54 +
55 + if (!error.response) {
56 + return __(
57 + 'No response came back, so this could not be confirmed. The change may already have been saved — ' +
58 + 'check in another tab before trying again.',
59 + 'code-snippets'
60 + )
61 + }
62 +
63 + const data: unknown = error.response.data
64 + const code = data && 'object' === typeof data && 'code' in data ? String(data.code) : ''
65 +
66 + if ('rest_cookie_invalid_nonce' === code || 'rest_not_logged_in' === code) {
67 + return __(
68 + 'You have been signed out, so nothing was saved. Sign in again in another tab, then save. Your changes are still here.',
69 + 'code-snippets'
70 + )
71 + }
72 +
73 + return unpackErrorResponse(error)
3 74 }