channel-rpc
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -1,2 +0,2 @@ | ||
export declare const Errors: { | ||
export declare const ChannelErrors: { | ||
readonly InvalidRequest: { | ||
@@ -3,0 +3,0 @@ readonly code: -32600; |
@@ -38,3 +38,3 @@ const MessageTypes = { | ||
const TIMEOUT_ERROR_MSG = "timeout"; | ||
export const Errors = { | ||
export const ChannelErrors = { | ||
InvalidRequest: { | ||
@@ -151,3 +151,3 @@ code: -32600, | ||
if (!isJsonRpcRequest(payload)) { | ||
const res = createErrorResponse(Errors.InvalidRequest, payload.id || null); | ||
const res = createErrorResponse(ChannelErrors.InvalidRequest, payload.id || null); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] reply`, res); | ||
@@ -160,3 +160,3 @@ this._sendResponse(source, res); | ||
if (!handler) { | ||
const res = createErrorResponse(Errors.MethodNotFound, payload.id || null); | ||
const res = createErrorResponse(ChannelErrors.MethodNotFound, payload.id || null); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, res); | ||
@@ -177,3 +177,3 @@ this._sendResponse(source, res); | ||
catch (err) { | ||
const res = createErrorResponse(Errors.InternalError, payload.id || null); | ||
const res = createErrorResponse(ChannelErrors.InternalError, payload.id || null); | ||
debug(`[CHANNEL_RPC_SERVER][channel=${this.channelId}] SEND_RESPONSE`, res); | ||
@@ -224,3 +224,3 @@ this._sendResponse(source, res); | ||
if (err.message === TIMEOUT_ERROR_MSG) { | ||
throw createErrorResponse(Errors.Timeout, id); | ||
throw createErrorResponse(ChannelErrors.Timeout, id); | ||
} | ||
@@ -227,0 +227,0 @@ throw err; |
{ | ||
"name": "channel-rpc", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "JSON-RPC over postMessage", | ||
@@ -5,0 +5,0 @@ "author": "kouhin", |
@@ -13,2 +13,3 @@ # channel-rpc: JSON-RPC over `postMessage` | ||
- **Controlled Communication**: Utilize the `start()` and `stop()` methods to manage when the server accepts messages. | ||
- **Error Handling**: Error responses from `client.stub` conform to the JSON-RPC standard, including well-defined error codes and messages. | ||
@@ -51,3 +52,3 @@ ## Installation | ||
```typescript | ||
import { ChannelClient } from "channel-rpc"; | ||
import { ChannelClient, ChannelErrors } from "channel-rpc"; | ||
@@ -63,3 +64,11 @@ import type { HandlerType } from "./main.ts"; | ||
// Use the stub to call methods on the main window | ||
const result = await client.stub.add(2, 3); // result === 5 | ||
try { | ||
const result = await client.stub.add(2, 3); | ||
} catch (error) { | ||
if (error.code === ChannelErrors.MethodNotFound.code) { | ||
// Handle the "Method not found" error | ||
} else if (error.code === ChannelErrors.InvalidRequest.code) { | ||
// Handle the "Invalid Request" error | ||
} | ||
} | ||
``` | ||
@@ -82,2 +91,9 @@ | ||
### `ChannelErrors` | ||
- `InvalidRequest`: Error object representing an "Invalid Request." | ||
- `MethodNotFound`: Error object representing a "Method not found." | ||
- `InternalError`: Error object representing an "Internal error." | ||
- `Timeout`: Error object representing a "Timeout." | ||
## License | ||
@@ -84,0 +100,0 @@ |
@@ -85,3 +85,3 @@ interface JsonRpcRequest { | ||
export const Errors = { | ||
export const ChannelErrors = { | ||
InvalidRequest: { | ||
@@ -106,3 +106,3 @@ code: -32600, | ||
function createErrorResponse( | ||
err: (typeof Errors)[keyof typeof Errors], | ||
err: (typeof ChannelErrors)[keyof typeof ChannelErrors], | ||
id: string | null | ||
@@ -241,3 +241,3 @@ ): JsonRpcErrorResponse { | ||
const res: JsonRpcErrorResponse = createErrorResponse( | ||
Errors.InvalidRequest, | ||
ChannelErrors.InvalidRequest, | ||
(payload as any).id || null | ||
@@ -258,3 +258,3 @@ ); | ||
const res: JsonRpcErrorResponse = createErrorResponse( | ||
Errors.MethodNotFound, | ||
ChannelErrors.MethodNotFound, | ||
payload.id || null | ||
@@ -283,3 +283,3 @@ ); | ||
const res: JsonRpcErrorResponse = createErrorResponse( | ||
Errors.InternalError, | ||
ChannelErrors.InternalError, | ||
payload.id || null | ||
@@ -365,3 +365,3 @@ ); | ||
if (err.message === TIMEOUT_ERROR_MSG) { | ||
throw createErrorResponse(Errors.Timeout, id); | ||
throw createErrorResponse(ChannelErrors.Timeout, id); | ||
} | ||
@@ -368,0 +368,0 @@ throw err; |
26516
116