@doist/comms-sdk 3.2.0 → 3.3.1
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/dist/esm/types/errors.js +74 -0
- package/dist/types/types/errors.d.ts +51 -0
- package/package.json +2 -2
package/dist/esm/types/errors.js
CHANGED
|
@@ -6,3 +6,77 @@ export class CommsRequestError extends CustomError {
|
|
|
6
6
|
this.responseData = responseData;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
function hasStatus(error, status) {
|
|
10
|
+
return error instanceof CommsRequestError && error.httpStatusCode === status;
|
|
11
|
+
}
|
|
12
|
+
function getResponseField(error, field) {
|
|
13
|
+
if (!(error instanceof CommsRequestError))
|
|
14
|
+
return undefined;
|
|
15
|
+
const data = error.responseData;
|
|
16
|
+
if (typeof data !== 'object' || data === null || !(field in data))
|
|
17
|
+
return undefined;
|
|
18
|
+
return data[field];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The numeric `error_code` the API sends in an error body, when there was one.
|
|
22
|
+
*
|
|
23
|
+
* @param error - The thrown value to inspect.
|
|
24
|
+
* @returns The code, or `null` when the error is not a
|
|
25
|
+
* {@link CommsRequestError} or carried no numeric `error_code`.
|
|
26
|
+
*/
|
|
27
|
+
export function getCommsErrorCode(error) {
|
|
28
|
+
const code = getResponseField(error, 'error_code');
|
|
29
|
+
return typeof code === 'number' ? code : null;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The `error_string` the API sends alongside `error_code`, when there was one.
|
|
33
|
+
* It is a server-authored message rather than a stable contract, so branch on
|
|
34
|
+
* {@link getCommsErrorCode} and use this for display.
|
|
35
|
+
*
|
|
36
|
+
* @param error - The thrown value to inspect.
|
|
37
|
+
* @returns The message, or `null` when the error is not a
|
|
38
|
+
* {@link CommsRequestError} or carried no string `error_string`.
|
|
39
|
+
*/
|
|
40
|
+
export function getCommsErrorString(error) {
|
|
41
|
+
const message = getResponseField(error, 'error_string');
|
|
42
|
+
return typeof message === 'string' ? message : null;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* True when the request failed with a 404.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* try {
|
|
50
|
+
* await api.threads.getThread(id)
|
|
51
|
+
* } catch (error) {
|
|
52
|
+
* if (isNotFound(error)) return null
|
|
53
|
+
* throw error
|
|
54
|
+
* }
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export function isNotFound(error) {
|
|
58
|
+
return hasStatus(error, 404);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* True when the request failed with a 409. The API uses this both for genuine
|
|
62
|
+
* conflicts and for a malformed id, so narrow with {@link isMalformedId}
|
|
63
|
+
* before reporting one as the other.
|
|
64
|
+
*/
|
|
65
|
+
export function isConflict(error) {
|
|
66
|
+
return hasStatus(error, 409);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* True when the API rejected an id as malformed: a 409 carrying `error_code`
|
|
70
|
+
* 217, which it sends both for a value that does not base58-decode to 16 bytes
|
|
71
|
+
* and for one whose version nibble is not 7. That is a bad reference rather
|
|
72
|
+
* than a conflict, so it usually deserves a different message from
|
|
73
|
+
* {@link isConflict}.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```typescript
|
|
77
|
+
* if (isMalformedId(error)) throw new Error(`Not a valid Comms id: ${ref}`)
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export function isMalformedId(error) {
|
|
81
|
+
return isConflict(error) && getCommsErrorCode(error) === 217;
|
|
82
|
+
}
|
|
@@ -4,3 +4,54 @@ export declare class CommsRequestError extends CustomError {
|
|
|
4
4
|
responseData?: unknown;
|
|
5
5
|
constructor(message: string, httpStatusCode?: number, responseData?: unknown, options?: ErrorOptions);
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* The numeric `error_code` the API sends in an error body, when there was one.
|
|
9
|
+
*
|
|
10
|
+
* @param error - The thrown value to inspect.
|
|
11
|
+
* @returns The code, or `null` when the error is not a
|
|
12
|
+
* {@link CommsRequestError} or carried no numeric `error_code`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getCommsErrorCode(error: unknown): number | null;
|
|
15
|
+
/**
|
|
16
|
+
* The `error_string` the API sends alongside `error_code`, when there was one.
|
|
17
|
+
* It is a server-authored message rather than a stable contract, so branch on
|
|
18
|
+
* {@link getCommsErrorCode} and use this for display.
|
|
19
|
+
*
|
|
20
|
+
* @param error - The thrown value to inspect.
|
|
21
|
+
* @returns The message, or `null` when the error is not a
|
|
22
|
+
* {@link CommsRequestError} or carried no string `error_string`.
|
|
23
|
+
*/
|
|
24
|
+
export declare function getCommsErrorString(error: unknown): string | null;
|
|
25
|
+
/**
|
|
26
|
+
* True when the request failed with a 404.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* try {
|
|
31
|
+
* await api.threads.getThread(id)
|
|
32
|
+
* } catch (error) {
|
|
33
|
+
* if (isNotFound(error)) return null
|
|
34
|
+
* throw error
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function isNotFound(error: unknown): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* True when the request failed with a 409. The API uses this both for genuine
|
|
41
|
+
* conflicts and for a malformed id, so narrow with {@link isMalformedId}
|
|
42
|
+
* before reporting one as the other.
|
|
43
|
+
*/
|
|
44
|
+
export declare function isConflict(error: unknown): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* True when the API rejected an id as malformed: a 409 carrying `error_code`
|
|
47
|
+
* 217, which it sends both for a value that does not base58-decode to 16 bytes
|
|
48
|
+
* and for one whose version nibble is not 7. That is a bad reference rather
|
|
49
|
+
* than a conflict, so it usually deserves a different message from
|
|
50
|
+
* {@link isConflict}.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* if (isMalformedId(error)) throw new Error(`Not a valid Comms id: ${ref}`)
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function isMalformedId(error: unknown): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doist/comms-sdk",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "A TypeScript wrapper for the Comms REST API.",
|
|
5
5
|
"author": "Doist developers",
|
|
6
6
|
"homepage": "https://doist.github.io/comms-sdk-typescript/",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@doist/sdk-kmp": "0.2.3",
|
|
46
46
|
"camelcase": "9.0.0",
|
|
47
47
|
"ts-custom-error": "3.3.1",
|
|
48
|
-
"undici": "
|
|
48
|
+
"undici": "8.11.2",
|
|
49
49
|
"uuid": "11.1.1",
|
|
50
50
|
"zod": "4.4.3"
|
|
51
51
|
},
|