Tom
Tom
TtRPC
Created by Tom on 8/12/2023 in #❓-help
Return TRPC Error from NextJS middleware
To be honest, I never got a great answer. So I just manually worked around it on the client side. So if I ever got a json error from my service I assume it was the middleware rejecting it
39 replies
TtRPC
Created by Tom on 11/4/2023 in #❓-help
is there a way to do client-side "middleware"?
unless, are you saying theres a function that will set headers before?
7 replies
TtRPC
Created by Tom on 11/4/2023 in #❓-help
is there a way to do client-side "middleware"?
im not sure how that solves the problem. that would allow me to send headers to the server but i need to call this function on the client
7 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
is that accurate?
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
as far as i can tell, i cant really do that
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
but then i want to enforce a rule in my error formatter that says 'for security reasons, i dont want to return UNAUTHORIZED because that tells the client that this resource exists. I want to convert this to NOT_FOUND'
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
lets say I have TRPCError({message: "You aren't allowed to see this", code: 'UNAUTHORIZED'})
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
i guess a simpler, but still practical, example would be like
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
but even if i know all the parameters that i would pass to a TRPCError, once its in the error formatter it seems that I lose the ability to replicate the same exact shape
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
if i only ever threw TRPCErrors then everything would work fine (which is what my app has been doing up to this point)
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
the problem im having is that i cant figure out how to take a non-trpc error in the error formatter and make it the same shape as a native trpc error
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
I'm not trying to think in HTTP. openapi-trpc should do that for me (at least thats the theory)
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
im also not sure if im handling stack correctly. will it still properly get omitted in production builds?
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
it seems that the TRPCError.code variable is also readonly, do i cant change it
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
but this still doesnt change the code that is actually sent down in the body:
curl -v localhost:3000/api/v1/test
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/v1/test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.1.2
> Accept: */*
>
< HTTP/1.1 404 Not Found # <------------------------------------------ CORRECT ERROR CODE FROM APP
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< Vary: Accept-Encoding
< Date: Thu, 19 Oct 2023 06:07:18 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
{"message":"test","code":"INTERNAL_SERVER_ERROR"}% # <----------------- ALWAYS INTERNAL_SERVER_ERROR
curl -v localhost:3000/api/v1/test
* Trying 127.0.0.1:3000...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /api/v1/test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/8.1.2
> Accept: */*
>
< HTTP/1.1 404 Not Found # <------------------------------------------ CORRECT ERROR CODE FROM APP
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< Vary: Accept-Encoding
< Date: Thu, 19 Oct 2023 06:07:18 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
{"message":"test","code":"INTERNAL_SERVER_ERROR"}% # <----------------- ALWAYS INTERNAL_SERVER_ERROR
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
responseMeta: (opts) => {
if (opts.errors[0]?.cause instanceof BaseError) {
return { status: opts.errors[0].cause.getHttpStatusCode() };
} else {
return { status: 500 };
}
},
responseMeta: (opts) => {
if (opts.errors[0]?.cause instanceof BaseError) {
return { status: opts.errors[0].cause.getHttpStatusCode() };
} else {
return { status: 500 };
}
},
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
but trpc-openapi doesnt completely work. I can change the http status code with this responseMeta() call:
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
ok so after a bunch of trying things im most of the way there with this code:
errorFormatter(params) {
if (params.error.cause && params.error.cause instanceof BaseError) {
const httpCode = params.error.cause.getHttpStatusCode();

return {
message: params.error.cause.displayMessage ?? "Internal Server Error",
code: getJsonRpcErrorCode(httpCode),
data: {
code: params.error.cause.code,
httpStatus: httpCode,
path: params.shape.data.path,
stack: params.shape.data.stack,
},
};
} else {
return {
message: "Internal Server Error",
code: getJsonRpcErrorCode(500),
data: {
code: params.error.code,
httpStatus: 500,
path: params.shape.data.path,
stack: params.shape.data.path,
},
};
}
},
errorFormatter(params) {
if (params.error.cause && params.error.cause instanceof BaseError) {
const httpCode = params.error.cause.getHttpStatusCode();

return {
message: params.error.cause.displayMessage ?? "Internal Server Error",
code: getJsonRpcErrorCode(httpCode),
data: {
code: params.error.cause.code,
httpStatus: httpCode,
path: params.shape.data.path,
stack: params.shape.data.stack,
},
};
} else {
return {
message: "Internal Server Error",
code: getJsonRpcErrorCode(500),
data: {
code: params.error.code,
httpStatus: 500,
path: params.shape.data.path,
stack: params.shape.data.path,
},
};
}
},
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
but in trpc-openapi i always get a 500 if the original error isnt a TRPCError
29 replies
TtRPC
Created by Tom on 10/18/2023 in #❓-help
working with custom errors and trpc errorFormatter
this seems to work in regular trpc (although im still not sure how i should be generating all these codes)
29 replies