Skip to content

Commit

Permalink
fix(errors): respect formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 13, 2023
1 parent dd4ee08 commit 8324a52
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
4 changes: 3 additions & 1 deletion src/adapter/chrome.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -66,7 +67,8 @@ export const createChromeHandler = <TRouter extends AnyRouter>(

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,
Expand Down
29 changes: 11 additions & 18 deletions src/adapter/errors.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 3 additions & 1 deletion src/adapter/window.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -72,7 +73,8 @@ export const createWindowHandler = <TRouter extends AnyRouter>(

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,
Expand Down
12 changes: 7 additions & 5 deletions src/link/internal/base.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -16,6 +16,7 @@ export const createBaseLink = <TRouter extends AnyRouter>(
const { id, type, path } = op;

try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const input = runtime.transformer.serialize(op.input);

const onDisconnect = () => {
Expand All @@ -31,19 +32,19 @@ export const createBaseLink = <TRouter extends AnyRouter>(
if (id !== trpc.id) return;

if ('error' in trpc) {
observer.error(TRPCClientError.from(trpc));
return;
return observer.error(TRPCClientError.from(trpc));
}

observer.next({
result: {
...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<TRouter>);

if (type !== 'subscription' || trpc.result.type === 'stopped') {
observer.complete();
Expand All @@ -58,6 +59,7 @@ export const createBaseLink = <TRouter extends AnyRouter>(
id,
jsonrpc: undefined,
method: type,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
params: { path, input },
},
} as TRPCChromeRequest);
Expand Down

0 comments on commit 8324a52

Please sign in to comment.