@@ -244,8 +244,21 @@ export function toRivetError(
244244 ) ;
245245}
246246
247- export function encodeBridgeRivetError ( error : RivetErrorLike ) : string {
248- return `${ BRIDGE_RIVET_ERROR_PREFIX } ${ JSON . stringify ( {
247+ /** A structured error encoded for the bridge to Core. */
248+ export interface EncodedBridgeError {
249+ readonly encoded : string ;
250+ /** Set when JSON could not encode the metadata, which was dropped. */
251+ readonly droppedMetadataCause ?: unknown ;
252+ }
253+
254+ /**
255+ * Encodes a structured error for the bridge to Core. Metadata that JSON cannot
256+ * encode, such as a `bigint`, is dropped so the error itself still crosses.
257+ */
258+ export function encodeBridgeRivetError (
259+ error : RivetErrorLike ,
260+ ) : EncodedBridgeError {
261+ const payload = {
249262 group : error . group ,
250263 code : error . code ,
251264 message : error . message ,
@@ -254,7 +267,17 @@ export function encodeBridgeRivetError(error: RivetErrorLike): string {
254267 public : error . public ,
255268 statusCode : error . statusCode ,
256269 actor : error . actor ,
257- } ) } `;
270+ } ;
271+ try {
272+ return {
273+ encoded : `${ BRIDGE_RIVET_ERROR_PREFIX } ${ JSON . stringify ( payload ) } ` ,
274+ } ;
275+ } catch ( cause ) {
276+ return {
277+ encoded : `${ BRIDGE_RIVET_ERROR_PREFIX } ${ JSON . stringify ( { ...payload , metadata : undefined } ) } ` ,
278+ droppedMetadataCause : cause ,
279+ } ;
280+ }
258281}
259282
260283/**
@@ -264,7 +287,7 @@ export function encodeBridgeRivetError(error: RivetErrorLike): string {
264287 */
265288export function encodeErrorForBridge ( error : unknown ) : string {
266289 if ( isCanonicalStructuredRivetError ( error ) ) {
267- return encodeBridgeRivetError ( error ) ;
290+ return encodeBridgeRivetError ( error ) . encoded ;
268291 }
269292 return String ( error ) ;
270293}
0 commit comments