-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e94ed78
commit e34aedd
Showing
10 changed files
with
85 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* api/utils/common */ | ||
import { FetchBaseQueryError } from '@reduxjs/toolkit/query/react'; | ||
/* | ||
JSONRPC Specification details | ||
JSON-RPC 1.0 - https://www.jsonrpc.org/specification_v1 | ||
JSON-RPC 1.1 wd - https://jsonrpc.org/historical/json-rpc-1-1-wd.html | ||
JSON-RPC 2.0 - https://www.jsonrpc.org/specification | ||
- id | ||
- 2.0 allows id to be string, number (with no fractional part) or null | ||
- 1.1 allows id to be "any JSON type" | ||
- version | ||
- a string in both JSONRPC 1.1 and 2.0. | ||
*/ | ||
// KBase mostly uses strings, or string serializable values, so we can too. | ||
type JsonRpcError = { | ||
version: '1.1'; | ||
id: string; | ||
error: { | ||
name: string; | ||
code: number; | ||
message: string; | ||
}; | ||
}; | ||
|
||
export type KBaseBaseQueryError = | ||
| FetchBaseQueryError | ||
| { | ||
status: 'JSONRPC_ERROR'; | ||
data: JsonRpcError; | ||
}; | ||
|
||
export const isJsonRpcError = (obj: unknown): obj is JsonRpcError => { | ||
if ( | ||
typeof obj === 'object' && | ||
obj !== null && | ||
['version', 'error', 'id'].every((k) => k in obj) | ||
) { | ||
const { version, error } = obj as { version: string; error: unknown }; | ||
const versionsSupported = new Set(['1.1', '2.0']); | ||
if (!versionsSupported.has(version)) return false; | ||
if ( | ||
typeof error === 'object' && | ||
error !== null && | ||
['name', 'code', 'message'].every((k) => k in error) | ||
) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* Type predicate to narrow an unknown error to `FetchBaseQueryError` | ||
*/ | ||
export function isFetchBaseQueryError( | ||
error: unknown | ||
): error is FetchBaseQueryError { | ||
return typeof error === 'object' && error !== null && 'status' in error; | ||
} | ||
|
||
export const isKBaseBaseQueryError = ( | ||
error: unknown | ||
): error is KBaseBaseQueryError => { | ||
const fbq = isFetchBaseQueryError(error); | ||
const condition = fbq && isJsonRpcError(error.data); | ||
return condition; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters