@oxyhq/core 19.1.2 → 20.0.0
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/LICENSE +202 -0
- package/NOTICE +15 -0
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/HttpService.js +23 -18
- package/dist/cjs/i18n/accountCategoryLabels.js +44 -0
- package/dist/cjs/i18n/accountRoleLabels.js +27 -0
- package/dist/cjs/i18n/reputationCategoryLabels.js +20 -0
- package/dist/cjs/i18n/trustTierLabels.js +19 -0
- package/dist/cjs/index.js +19 -9
- package/dist/cjs/mixins/OxyServices.followGraph.js +17 -0
- package/dist/cjs/session/accountProjection.js +31 -6
- package/dist/cjs/utils/errorUtils.js +65 -1
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/HttpService.js +24 -19
- package/dist/esm/i18n/accountCategoryLabels.js +37 -0
- package/dist/esm/i18n/accountRoleLabels.js +20 -0
- package/dist/esm/i18n/reputationCategoryLabels.js +13 -0
- package/dist/esm/i18n/trustTierLabels.js +12 -0
- package/dist/esm/index.js +11 -8
- package/dist/esm/mixins/OxyServices.followGraph.js +17 -0
- package/dist/esm/session/accountProjection.js +30 -6
- package/dist/esm/utils/errorUtils.js +63 -1
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/i18n/accountCategoryLabels.d.ts +34 -0
- package/dist/types/i18n/accountRoleLabels.d.ts +10 -0
- package/dist/types/i18n/reputationCategoryLabels.d.ts +10 -0
- package/dist/types/i18n/trustTierLabels.d.ts +9 -0
- package/dist/types/index.d.ts +7 -2
- package/dist/types/mixins/OxyServices.followGraph.d.ts +13 -0
- package/dist/types/session/accountProjection.d.ts +20 -4
- package/dist/types/utils/errorUtils.d.ts +67 -0
- package/package.json +7 -6
- package/src/HttpService.ts +29 -22
- package/src/__tests__/parseHttpErrorBody.test.ts +116 -0
- package/src/__tests__/serverValueImportsDeclared.test.ts +7 -0
- package/src/i18n/__tests__/accountCategoryLabels.test.ts +62 -0
- package/src/i18n/__tests__/accountRoleLabels.test.ts +54 -0
- package/src/i18n/__tests__/reputationCategoryLabels.test.ts +56 -0
- package/src/i18n/__tests__/trustTierLabels.test.ts +47 -0
- package/src/i18n/accountCategoryLabels.ts +44 -0
- package/src/i18n/accountRoleLabels.ts +26 -0
- package/src/i18n/reputationCategoryLabels.ts +20 -0
- package/src/i18n/trustTierLabels.ts +18 -0
- package/src/index.ts +13 -6
- package/src/mixins/OxyServices.followGraph.ts +24 -0
- package/src/mixins/__tests__/followGraph.test.ts +19 -0
- package/src/session/__tests__/accountProjection.test.ts +98 -0
- package/src/session/accountProjection.ts +37 -6
- package/src/utils/errorUtils.ts +116 -5
package/dist/esm/HttpService.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
import { TTLCache, registerCacheForCleanup } from './utils/cache.js';
|
|
16
16
|
import { RequestDeduplicator, RequestQueue, SimpleLogger } from './utils/requestUtils.js';
|
|
17
17
|
import { retryAsync } from './utils/asyncUtils.js';
|
|
18
|
-
import { handleHttpError } from './utils/errorUtils.js';
|
|
18
|
+
import { handleHttpError, parseHttpErrorBody } from './utils/errorUtils.js';
|
|
19
19
|
import { jwtDecode } from 'jwt-decode';
|
|
20
20
|
import { isNative, getPlatformOS } from './utils/platform.js';
|
|
21
21
|
import { isReactNative } from '@oxyhq/protocol';
|
|
@@ -447,32 +447,37 @@ export class HttpService {
|
|
|
447
447
|
// Failed to parse error body — not a CSRF error
|
|
448
448
|
}
|
|
449
449
|
}
|
|
450
|
-
//
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
450
|
+
// Read the error body (may be absent, non-JSON, empty or malformed).
|
|
451
|
+
// Anything unreadable leaves `errorBody` undefined and degrades to the
|
|
452
|
+
// status-based message — an error path that throws its own error is
|
|
453
|
+
// worse than the error it was reporting.
|
|
454
|
+
let errorBody;
|
|
455
|
+
const errorContentType = response.headers.get('content-type');
|
|
456
|
+
if (errorContentType?.includes('application/json')) {
|
|
454
457
|
try {
|
|
455
|
-
|
|
456
|
-
// Accept either structured error field from API responses.
|
|
457
|
-
if (errorData?.message) {
|
|
458
|
-
errorMessage = errorData.message;
|
|
459
|
-
}
|
|
460
|
-
else if (errorData?.error_description) {
|
|
461
|
-
// RFC 6749 §5.2 / RFC 6750 §3 — OAuth endpoints surface human text here.
|
|
462
|
-
errorMessage = errorData.error_description;
|
|
463
|
-
}
|
|
464
|
-
else if (errorData?.error) {
|
|
465
|
-
errorMessage = errorData.error;
|
|
466
|
-
}
|
|
458
|
+
errorBody = await response.json();
|
|
467
459
|
}
|
|
468
460
|
catch (parseError) {
|
|
469
461
|
// Malformed JSON or empty response - use status text
|
|
470
462
|
this.logger.warn('Failed to parse error response JSON:', parseError);
|
|
471
463
|
}
|
|
472
464
|
}
|
|
473
|
-
|
|
465
|
+
// `parseHttpErrorBody` handles every envelope in use, including the
|
|
466
|
+
// nested `{ error: { code, message } }` shape — assigning that nested
|
|
467
|
+
// OBJECT as the message is what produced `"[object Object]"`.
|
|
468
|
+
const parsed = parseHttpErrorBody(errorBody);
|
|
469
|
+
const error = new Error(parsed.message ?? `HTTP ${response.status}: ${response.statusText}`);
|
|
474
470
|
error.status = response.status;
|
|
475
|
-
error.response = { status: response.status, statusText: response.statusText };
|
|
471
|
+
error.response = { status: response.status, statusText: response.statusText, data: errorBody };
|
|
472
|
+
// Only set `code`/`details` when the server actually sent them.
|
|
473
|
+
// Assigning `undefined` would still create the property, which changes
|
|
474
|
+
// how `handleHttpError` classifies the error downstream.
|
|
475
|
+
if (parsed.code !== undefined) {
|
|
476
|
+
error.code = parsed.code;
|
|
477
|
+
}
|
|
478
|
+
if (parsed.details !== undefined) {
|
|
479
|
+
error.details = parsed.details;
|
|
480
|
+
}
|
|
476
481
|
throw error;
|
|
477
482
|
}
|
|
478
483
|
// Handle different response types (optimized - read response once)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import enUS from './locales/en-US.json' with { type: "json" };
|
|
2
|
+
import { translate } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Every account category's English name, keyed by its stable id.
|
|
5
|
+
*
|
|
6
|
+
* **The annotation is the point.** The vocabulary lives in `@oxyhq/contracts`
|
|
7
|
+
* and the names live in `locales/en-US.json`, so they are two lists that must
|
|
8
|
+
* agree and nothing but a type can make them. Declaring the JSON node as a
|
|
9
|
+
* TOTAL `Record<AccountCategoryId, string>` turns "somebody added a category at
|
|
10
|
+
* Oxy and nobody wrote its English" into a `TS2741` naming the missing id, at
|
|
11
|
+
* build time, instead of a picker row that paints `accounts.accountCategory.<id>`
|
|
12
|
+
* at a user trying to choose one.
|
|
13
|
+
*
|
|
14
|
+
* That failure is not hypothetical. The screen previously wrote `t(key) || id`,
|
|
15
|
+
* whose author believed an unnamed id would degrade to its raw slug. It cannot:
|
|
16
|
+
* {@link translate} echoes the KEY when it resolves nothing, and a non-empty
|
|
17
|
+
* string is never falsy, so the `|| id` arm was unreachable and the output was
|
|
18
|
+
* the dotted key. A runtime fallback that cannot run is worse than none,
|
|
19
|
+
* because it reads as protection.
|
|
20
|
+
*
|
|
21
|
+
* Totality is over `ACCOUNT_CATEGORY_IDS`, which RETAINS withdrawn ids, so an
|
|
22
|
+
* account still carrying a retired category keeps rendering its name while no
|
|
23
|
+
* picker offers it again. Retired and unknown are different cases: only an id
|
|
24
|
+
* outside the union is unnameable, which is why this is keyed by
|
|
25
|
+
* `AccountCategoryId` and not by `string`.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Module-scoped, NOT re-exported from the package index: the annotation is the
|
|
29
|
+
* whole job, and it does that job without being public API. It carries no
|
|
30
|
+
* `Object.freeze` and no `Readonly<>` for the same reason — those existed only
|
|
31
|
+
* to make an exported reference safe from a consumer's stray write, and there
|
|
32
|
+
* is no such consumer. Exported from the MODULE so its own test can name it.
|
|
33
|
+
*/
|
|
34
|
+
export const EN_ACCOUNT_CATEGORY_LABELS = enUS.accounts.accountCategory;
|
|
35
|
+
export function accountCategoryLabel(locale, id) {
|
|
36
|
+
return translate(locale, `accounts.accountCategory.${id}`);
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import enUS from './locales/en-US.json' with { type: "json" };
|
|
2
|
+
import { translate } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Every account member role's English name, keyed by its stable id.
|
|
5
|
+
*
|
|
6
|
+
* Totality is over the closed `AccountRole` union so a new role without an
|
|
7
|
+
* English label is a build error, not a members row that paints
|
|
8
|
+
* `accounts.roles.<role>.label`.
|
|
9
|
+
*/
|
|
10
|
+
export const EN_ACCOUNT_ROLE_LABELS = {
|
|
11
|
+
owner: enUS.accounts.roles.owner.label,
|
|
12
|
+
admin: enUS.accounts.roles.admin.label,
|
|
13
|
+
editor: enUS.accounts.roles.editor.label,
|
|
14
|
+
developer: enUS.accounts.roles.developer.label,
|
|
15
|
+
billing: enUS.accounts.roles.billing.label,
|
|
16
|
+
viewer: enUS.accounts.roles.viewer.label,
|
|
17
|
+
};
|
|
18
|
+
export function accountRoleLabel(locale, role) {
|
|
19
|
+
return translate(locale, `accounts.roles.${role}.label`);
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import enUS from './locales/en-US.json' with { type: "json" };
|
|
2
|
+
import { translate } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Every reputation rule category's English name, keyed by its stable id.
|
|
5
|
+
*
|
|
6
|
+
* Totality is over `REPUTATION_CATEGORIES` from `@oxyhq/contracts` so a new
|
|
7
|
+
* category added server-side without an English label is a build error, not a
|
|
8
|
+
* Trust Rules section title that paints `trust.rules.categories.<id>`.
|
|
9
|
+
*/
|
|
10
|
+
export const EN_REPUTATION_CATEGORY_LABELS = enUS.trust.rules.categories;
|
|
11
|
+
export function reputationCategoryLabel(locale, id) {
|
|
12
|
+
return translate(locale, `trust.rules.categories.${id}`);
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import enUS from './locales/en-US.json' with { type: "json" };
|
|
2
|
+
import { translate } from './index.js';
|
|
3
|
+
/**
|
|
4
|
+
* Every trust tier's English name, keyed by its stable id.
|
|
5
|
+
*
|
|
6
|
+
* Totality is over `TRUST_TIERS` from `@oxyhq/contracts` so a new tier without
|
|
7
|
+
* an English label is a build error, not a chip that paints `trust.tiers.<id>`.
|
|
8
|
+
*/
|
|
9
|
+
export const EN_TRUST_TIER_LABELS = enUS.trust.tiers;
|
|
10
|
+
export function trustTierLabel(locale, tier) {
|
|
11
|
+
return translate(locale, `trust.tiers.${tier}`);
|
|
12
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -110,11 +110,15 @@ export { DEFAULT_CIRCUIT_BREAKER_CONFIG, createCircuitBreakerState, calculateBac
|
|
|
110
110
|
// i18n
|
|
111
111
|
// ---------------------------------------------------------------------------
|
|
112
112
|
export { translate } from './i18n/index.js';
|
|
113
|
+
export { accountCategoryLabel } from './i18n/accountCategoryLabels.js';
|
|
114
|
+
export { accountRoleLabel } from './i18n/accountRoleLabels.js';
|
|
115
|
+
export { reputationCategoryLabel } from './i18n/reputationCategoryLabels.js';
|
|
116
|
+
export { trustTierLabel } from './i18n/trustTierLabels.js';
|
|
113
117
|
// ---------------------------------------------------------------------------
|
|
114
118
|
// API request / URL helpers
|
|
115
119
|
// ---------------------------------------------------------------------------
|
|
116
120
|
export { buildQueryParams, buildSearchParams, buildUrl, buildPaginationParams, safeJsonParse, } from './utils/apiUtils.js';
|
|
117
|
-
export { ErrorCodes, createApiError, handleHttpError, validateRequiredFields, } from './utils/errorUtils.js';
|
|
121
|
+
export { ErrorCodes, createApiError, handleHttpError, isHttpRequestError, parseHttpErrorBody, validateRequiredFields, } from './utils/errorUtils.js';
|
|
118
122
|
export { retryAsync } from './utils/asyncUtils.js';
|
|
119
123
|
// ---------------------------------------------------------------------------
|
|
120
124
|
// Validation
|
|
@@ -173,13 +177,12 @@ export { deviceStateToClientSessions, activeSessionIdOf, activeUserOf, accountId
|
|
|
173
177
|
// chooser: device sign-ins ∪ account graph, deduped by accountId). Pure +
|
|
174
178
|
// I/O-free — the caller hydrates profiles via `getUsersByIds`. Shared by
|
|
175
179
|
// `@oxyhq/services` and auth.oxy.so so the list can't diverge.
|
|
176
|
-
// `isSwitchTargetAccount` is the
|
|
177
|
-
//
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
//
|
|
181
|
-
|
|
182
|
-
export { isSwitchTargetAccount, projectSwitchableAccounts, switchableAccountIds, } from './session/accountProjection.js';
|
|
180
|
+
// `isSwitchTargetAccount` is the structural half ("is this kind switchable at
|
|
181
|
+
// all?"); `canSwitchIntoAccount` adds the caller's `account:act_as` permission.
|
|
182
|
+
// Both are exported so surfaces that render `AccountNode`s rather than the
|
|
183
|
+
// projection — the Console workspace switcher, managed-accounts rows — ask the
|
|
184
|
+
// SAME questions instead of testing a kind literal.
|
|
185
|
+
export { isSwitchTargetAccount, canSwitchIntoAccount, projectSwitchableAccounts, switchableAccountIds, } from './session/accountProjection.js';
|
|
183
186
|
// Headless controller for the unified account dialog. Framework-agnostic
|
|
184
187
|
// state machine + subscribe/getSnapshot store (bind via `useSyncExternalStore`)
|
|
185
188
|
// — sign-in is passkey (WebAuthn) or the Commons QR / shared-keychain handoff;
|
|
@@ -156,6 +156,23 @@ export function OxyServicesFollowGraphMixin(Base) {
|
|
|
156
156
|
throw this.handleError(error);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Release a namespace the calling application holds, when nothing is
|
|
161
|
+
registered inside it yet.
|
|
162
|
+
*
|
|
163
|
+
* Idempotent when the namespace is already unowned (`released: false`).
|
|
164
|
+
* Exists because claims are first-come and registration runs on boot — a
|
|
165
|
+
* development build with the wrong client id can bind a name permanently
|
|
166
|
+
* unless the holder can give it back.
|
|
167
|
+
*/
|
|
168
|
+
async releaseFollowNamespace(namespace) {
|
|
169
|
+
try {
|
|
170
|
+
return await this.makeRequest('DELETE', `/v2/follow-targets/namespaces/${encodeURIComponent(namespace)}`, undefined, { cache: false });
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
throw this.handleError(error);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
159
176
|
/**
|
|
160
177
|
* Declare what following a kind of thing MEANS: the verb clients render,
|
|
161
178
|
* whether reverse lookups are public, whether it federates.
|
|
@@ -48,6 +48,30 @@ import { getNormalizedUserHandle } from '../utils/userHandle.js';
|
|
|
48
48
|
export function isSwitchTargetAccount(node) {
|
|
49
49
|
return node.relationship === 'self' || isActAsEligibleKind(node.kind);
|
|
50
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Whether the caller may switch INTO this account — the server-side
|
|
53
|
+
* `account:act_as` gate plus the structural {@link isSwitchTargetAccount} rule.
|
|
54
|
+
*
|
|
55
|
+
* `relationship: 'self'` always passes (returning to the caller's own personal
|
|
56
|
+
* account). Every other ground requires a switch-eligible kind AND
|
|
57
|
+
* `account:act_as` in the resolved membership permissions. When permissions are
|
|
58
|
+
* absent but the relationship is `owner`, the owner baseline is assumed — the
|
|
59
|
+
* API always resolves effective permissions for owned accounts, but test
|
|
60
|
+
* fixtures and stale rows may omit the membership blob.
|
|
61
|
+
*/
|
|
62
|
+
export function canSwitchIntoAccount(node) {
|
|
63
|
+
if (node.relationship === 'self') {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
if (!isSwitchTargetAccount(node)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const permissions = node.callerMembership?.permissions;
|
|
70
|
+
if (permissions) {
|
|
71
|
+
return permissions.includes('account:act_as');
|
|
72
|
+
}
|
|
73
|
+
return node.relationship === 'owner';
|
|
74
|
+
}
|
|
51
75
|
/**
|
|
52
76
|
* Pure union of device sign-ins and account-graph nodes into the flat
|
|
53
77
|
* {@link SwitchableAccount}[] every switcher renders.
|
|
@@ -57,9 +81,9 @@ export function isSwitchTargetAccount(node) {
|
|
|
57
81
|
* and a graph node is deduped into ONE device row enriched with the graph
|
|
58
82
|
* metadata (relationship / kind / parent / membership).
|
|
59
83
|
*
|
|
60
|
-
* Graph nodes
|
|
61
|
-
*
|
|
62
|
-
* below.
|
|
84
|
+
* Graph nodes the caller cannot switch into — a `channel`, or a managed account
|
|
85
|
+
* whose membership lacks `account:act_as` — are omitted.
|
|
86
|
+
* {@link canSwitchIntoAccount} is the rule; see the filter below.
|
|
63
87
|
*/
|
|
64
88
|
export function projectSwitchableAccounts(input) {
|
|
65
89
|
const { state, graph, profilesById, activeUser, locale, resolveAvatarUrl } = input;
|
|
@@ -139,7 +163,7 @@ export function projectSwitchableAccounts(input) {
|
|
|
139
163
|
// An account already on the device skipped this check via the branch above,
|
|
140
164
|
// and correctly: whatever its kind, the caller is signed into it, so
|
|
141
165
|
// switching is a local activation that asks the server for nothing.
|
|
142
|
-
if (!
|
|
166
|
+
if (!canSwitchIntoAccount(node)) {
|
|
143
167
|
continue;
|
|
144
168
|
}
|
|
145
169
|
remember(toRow(node.account, {
|
|
@@ -161,7 +185,7 @@ export function projectSwitchableAccounts(input) {
|
|
|
161
185
|
* document, but including their ids lets the caller pass one id set and lets the
|
|
162
186
|
* projection prefer freshly-fetched profiles uniformly.
|
|
163
187
|
*
|
|
164
|
-
* Applies the SAME {@link
|
|
188
|
+
* Applies the SAME {@link canSwitchIntoAccount} filter as
|
|
165
189
|
* {@link projectSwitchableAccounts} to graph nodes, so this never fetches a
|
|
166
190
|
* profile for a row the projection will drop — and, just as importantly, never
|
|
167
191
|
* SKIPS one the projection will keep, which would leave that row unrendered
|
|
@@ -175,7 +199,7 @@ export function switchableAccountIds(state, graph) {
|
|
|
175
199
|
}
|
|
176
200
|
}
|
|
177
201
|
for (const node of graph) {
|
|
178
|
-
if (node.accountId &&
|
|
202
|
+
if (node.accountId && canSwitchIntoAccount(node)) {
|
|
179
203
|
ids.add(node.accountId);
|
|
180
204
|
}
|
|
181
205
|
}
|
|
@@ -28,6 +28,63 @@ export const ErrorCodes = {
|
|
|
28
28
|
NETWORK_ERROR: 'NETWORK_ERROR',
|
|
29
29
|
CONNECTION_FAILED: 'CONNECTION_FAILED'
|
|
30
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* Narrow a caught value to {@link HttpRequestError}.
|
|
33
|
+
*
|
|
34
|
+
* Returns `false` for a plain {@link ApiError} object (those are objects, not
|
|
35
|
+
* `Error`s) — run an arbitrary thrown value through {@link handleHttpError}
|
|
36
|
+
* first if you need one normalized.
|
|
37
|
+
*/
|
|
38
|
+
export function isHttpRequestError(value) {
|
|
39
|
+
if (!(value instanceof Error)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return typeof value.status === 'number';
|
|
43
|
+
}
|
|
44
|
+
const isPlainRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
45
|
+
const nonEmptyString = (value) => typeof value === 'string' && value.trim().length > 0 ? value : undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Extract `message` / `code` / `details` from a parsed HTTP error response body.
|
|
48
|
+
*
|
|
49
|
+
* Handles every error envelope in use across the Oxy ecosystem:
|
|
50
|
+
*
|
|
51
|
+
* - `{ error: { code, message, details? } }` — nested envelope (CrowdSource and
|
|
52
|
+
* other Oxy services). Never stringify the nested object: `new Error(obj)`
|
|
53
|
+
* yields the literal message `"[object Object]"`.
|
|
54
|
+
* - `{ error: '<CODE>', message, details? }` — oxy-api's canonical shape
|
|
55
|
+
* (`ApiError.toJSON`), where the top-level `error` field IS the code.
|
|
56
|
+
* - `{ error: '<CODE>', error_description }` — RFC 6749 §5.2 / RFC 6750 §3, the
|
|
57
|
+
* OAuth token and userinfo endpoints. `error_description` is the human text
|
|
58
|
+
* and `error` is the machine code, so both survive.
|
|
59
|
+
* - `{ message, code }` — e.g. the API's CSRF rejections.
|
|
60
|
+
* - `{ error: '<human message>' }` — legacy hand-rolled routes. With no sibling
|
|
61
|
+
* `message`/`error_description` the string is the message, not a code: a bare
|
|
62
|
+
* `error` string is not machine-readable enough to promote to `code`.
|
|
63
|
+
*
|
|
64
|
+
* Anything else — a non-object body (`null`, `[]`, `"str"`, `42`), or an object
|
|
65
|
+
* carrying none of these fields — yields an empty result, leaving the caller on
|
|
66
|
+
* its status-based fallback message. Total function: never throws.
|
|
67
|
+
*/
|
|
68
|
+
export function parseHttpErrorBody(body) {
|
|
69
|
+
if (!isPlainRecord(body)) {
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
const nested = isPlainRecord(body.error) ? body.error : undefined;
|
|
73
|
+
const errorString = nonEmptyString(body.error);
|
|
74
|
+
// A sibling that proves the top-level `error` is a CODE rather than prose.
|
|
75
|
+
const siblingMessage = nonEmptyString(body.message) ?? nonEmptyString(body.error_description);
|
|
76
|
+
return {
|
|
77
|
+
message: siblingMessage ?? (nested ? nonEmptyString(nested.message) : errorString),
|
|
78
|
+
code: (nested ? nonEmptyString(nested.code) : undefined) ??
|
|
79
|
+
nonEmptyString(body.code) ??
|
|
80
|
+
(siblingMessage ? errorString : undefined),
|
|
81
|
+
details: isPlainRecord(body.details)
|
|
82
|
+
? body.details
|
|
83
|
+
: nested && isPlainRecord(nested.details)
|
|
84
|
+
? nested.details
|
|
85
|
+
: undefined,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
31
88
|
/**
|
|
32
89
|
* Create a standardized API error
|
|
33
90
|
*/
|
|
@@ -72,7 +129,12 @@ export function handleHttpError(error) {
|
|
|
72
129
|
const fetchError = error;
|
|
73
130
|
const status = fetchError.response?.status || fetchError.status;
|
|
74
131
|
if (status) {
|
|
75
|
-
|
|
132
|
+
// `details` is carried through when present: a body may ship structured
|
|
133
|
+
// detail without a machine-readable `code` (which is what routes the
|
|
134
|
+
// error to the already-an-ApiError branch above), and dropping it here
|
|
135
|
+
// would make it unreachable to every caller that rethrows via
|
|
136
|
+
// `OxyServices.handleError`.
|
|
137
|
+
return createApiError(fetchError.message || `HTTP ${status} error`, getErrorCodeFromStatus(status), status, isPlainRecord(fetchError.details) ? fetchError.details : undefined);
|
|
76
138
|
}
|
|
77
139
|
}
|
|
78
140
|
// Handle standard errors
|