From 8324a5274873785938e073d103804ade21ac7c26 Mon Sep 17 00:00:00 2001 From: Janek Date: Wed, 13 Dec 2023 11:44:08 +0100 Subject: [PATCH] fix(errors): respect formatter --- src/adapter/chrome.ts | 4 +++- src/adapter/errors.ts | 29 +++++++++++------------------ src/adapter/window.ts | 4 +++- src/link/internal/base.ts | 12 +++++++----- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/adapter/chrome.ts b/src/adapter/chrome.ts index 818b963..d7b0104 100644 --- a/src/adapter/chrome.ts +++ b/src/adapter/chrome.ts @@ -1,5 +1,6 @@ import { AnyProcedure, AnyRouter, TRPCError } from '@trpc/server'; import { Unsubscribable, isObservable } from '@trpc/server/observable'; +import { getErrorShape } from '@trpc/server/shared'; import { isTRPCRequestWithId } from '../shared/trpcMessage'; import type { TRPCChromeResponse } from '../types'; @@ -66,7 +67,8 @@ export const createChromeHandler = ( sendResponse({ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - error: router.getErrorShape({ + error: getErrorShape({ + config: router._def._config, error, type: method, path: params.path, diff --git a/src/adapter/errors.ts b/src/adapter/errors.ts index 002130e..5704da3 100644 --- a/src/adapter/errors.ts +++ b/src/adapter/errors.ts @@ -1,27 +1,20 @@ import { TRPCError } from '@trpc/server'; export function getErrorFromUnknown(cause: unknown): TRPCError { - if (cause instanceof Error && cause.name === 'TRPCError') { - return cause as TRPCError; - } - - let errorCause: Error | undefined = undefined; - let stack: string | undefined = undefined; - if (cause instanceof Error) { - errorCause = cause; - stack = cause.stack; + if (cause.name === 'TRPCError') { + return cause as TRPCError; + } + const error = new TRPCError({ + message: 'Internal server error', + code: 'INTERNAL_SERVER_ERROR', + cause: cause, + }); + error.stack = cause.stack; + return error; } - - const error = new TRPCError({ + return new TRPCError({ message: 'Internal server error', code: 'INTERNAL_SERVER_ERROR', - cause: errorCause, }); - - if (stack) { - error.stack = stack; - } - - return error; } diff --git a/src/adapter/window.ts b/src/adapter/window.ts index e24f552..1e63f8f 100644 --- a/src/adapter/window.ts +++ b/src/adapter/window.ts @@ -1,5 +1,6 @@ import { AnyProcedure, AnyRouter, TRPCError } from '@trpc/server'; import { Unsubscribable, isObservable } from '@trpc/server/observable'; +import { getErrorShape } from '@trpc/server/shared'; import { TRPC_BROWSER_LOADED_EVENT } from '../shared/constants'; import { isTRPCRequestWithId } from '../shared/trpcMessage'; @@ -72,7 +73,8 @@ export const createWindowHandler = ( sendResponse({ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - error: router.getErrorShape({ + error: getErrorShape({ + config: router._def._config, error, type: method, path: params.path, diff --git a/src/link/internal/base.ts b/src/link/internal/base.ts index 90a49c8..d9fde69 100644 --- a/src/link/internal/base.ts +++ b/src/link/internal/base.ts @@ -1,4 +1,4 @@ -import { TRPCClientError, TRPCLink } from '@trpc/client'; +import { OperationResultEnvelope, TRPCClientError, TRPCLink } from '@trpc/client'; import type { AnyRouter } from '@trpc/server'; import { observable } from '@trpc/server/observable'; @@ -16,6 +16,7 @@ export const createBaseLink = ( const { id, type, path } = op; try { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const input = runtime.transformer.serialize(op.input); const onDisconnect = () => { @@ -31,8 +32,7 @@ export const createBaseLink = ( if (id !== trpc.id) return; if ('error' in trpc) { - observer.error(TRPCClientError.from(trpc)); - return; + return observer.error(TRPCClientError.from(trpc)); } observer.next({ @@ -40,10 +40,11 @@ export const createBaseLink = ( ...trpc.result, ...((!trpc.result.type || trpc.result.type === 'data') && { type: 'data', + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment data: runtime.transformer.deserialize(trpc.result.data), }), - } as any, - }); + }, + } as OperationResultEnvelope); if (type !== 'subscription' || trpc.result.type === 'stopped') { observer.complete(); @@ -58,6 +59,7 @@ export const createBaseLink = ( id, jsonrpc: undefined, method: type, + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment params: { path, input }, }, } as TRPCChromeRequest);