Skip to content

Commit

Permalink
Merge pull request #59 from unstubbable/call-server-error
Browse files Browse the repository at this point in the history
Expose HTTP status codes for failed server actions
  • Loading branch information
unstubbable authored Mar 15, 2024
2 parents d7d165c + c020f2d commit 1bd9fbf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/core/src/client/call-server-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export class CallServerError extends Error {
#statusCode: number;

constructor(message: string, statusCode: number) {
super(message);
this.#statusCode = statusCode;
}

get statusCode(): number {
return this.#statusCode;
}
}
7 changes: 7 additions & 0 deletions packages/core/src/client/call-server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {ReactServerValue} from 'react-server-dom-webpack';
import ReactServerDOMClient from 'react-server-dom-webpack/client.browser';
import {CallServerError} from './call-server-error.js';

export async function callServer(
id: string,
Expand All @@ -10,6 +11,12 @@ export async function callServer(
method: `POST`,
headers: {'accept': `text/x-component`, 'x-rsc-action': id},
body: await ReactServerDOMClient.encodeReply(args),
}).then((response) => {
if (response.ok) {
return response;
}

throw new CallServerError(response.statusText, response.status);
}),
);
}
1 change: 1 addition & 0 deletions packages/core/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './call-server-error.js';
export * from './link.js';
export * from './use-router.js';

0 comments on commit 1bd9fbf

Please sign in to comment.