Skip to content

Commit 9a27142

Browse files
Enhance error handling in RSC modules by introducing extractErrorMessage utility
1 parent e8ebbc2 commit 9a27142

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

node_package/src/RSCRequestTracker.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { PassThrough, Readable } from 'stream';
2+
import { extractErrorMessage } from './utils.ts';
23
import {
34
RSCPayloadStreamInfo,
45
RSCPayloadCallback,
@@ -141,9 +142,7 @@ class RSCRequestTracker {
141142
} catch (error) {
142143
// Provide a more helpful error message that includes context
143144
throw new Error(
144-
`Failed to generate RSC payload for component "${componentName}": ${
145-
error instanceof Error ? error.message : String(error)
146-
}`,
145+
`Failed to generate RSC payload for component "${componentName}": ${extractErrorMessage(error)}`,
147146
);
148147
}
149148
}

node_package/src/getReactServerComponent.client.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { createFromReadableStream } from 'react-on-rails-rsc/client.browser';
3-
import { createRSCPayloadKey, fetch, wrapInNewPromise } from './utils.ts';
3+
import { createRSCPayloadKey, fetch, wrapInNewPromise, extractErrorMessage } from './utils.ts';
44
import transformRSCStreamAndReplayConsoleLogs from './transformRSCStreamAndReplayConsoleLogs.ts';
55
import { RailsContext } from './types/index.ts';
66

@@ -65,17 +65,13 @@ const fetchRSC = ({
6565

6666
return createFromFetch(fetch(fetchUrl)).catch((error: unknown) => {
6767
throw new Error(
68-
`Failed to fetch RSC payload for component "${componentName}" from "${fetchUrl}": ${
69-
error instanceof Error ? error.message : String(error)
70-
}`,
68+
`Failed to fetch RSC payload for component "${componentName}" from "${fetchUrl}": ${extractErrorMessage(error)}`,
7169
);
7270
});
7371
} catch (error: unknown) {
7472
// Handle JSON.stringify errors or other synchronous errors
7573
throw new Error(
76-
`Failed to prepare RSC request for component "${componentName}": ${
77-
error instanceof Error ? error.message : String(error)
78-
}`,
74+
`Failed to prepare RSC request for component "${componentName}": ${extractErrorMessage(error)}`,
7975
);
8076
}
8177
};

node_package/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ export const wrapInNewPromise = <T>(promise: Promise<T>) => {
4040
void promise.catch(reject);
4141
});
4242
};
43+
44+
export const extractErrorMessage = (error: unknown): string => {
45+
return error instanceof Error ? error.message : String(error);
46+
};

0 commit comments

Comments
 (0)