Custom Zod errors in tRPC - next

How can I set up tRPC so that when zod throws an error I can handle it instead of tRPC. I have looked everywhere for an answer and I can't find one

Related

anyhow: Return nested/wrapped errors

use anyhow::Context;
fancy_module::run()
.await
.with_context(|| {
format!("An error has been found")
})?;
From what I understand, when run returns an error, we return "An error has been found". But this message is not really meaningful. I would like to also return the error that run returns. Something like format!("An error has been found {}", e). How do I get e returned by run?
I could do that in multiple lines of code. By fetching the result of run and then having a match statement. Is there a nicer way of doing that?
From what I understand, when run returns an error, we return "An error has been found".
Not correct! context and with_context create a wrapper around the underlying error value to introduce additional context, but the original error is kept within!
Once you try displaying the error, you will see something of this sort:
Error: An error has been found
Caused by:
No such file or directory (os error 2)
(This also shows that "An error has been found" is a poor choice of words for creating additional context.)
I would like to also return the error that run returns. Something like format!("An error has been found {}", e).
That used to be a reasonable design decision in the past, but the current guidelines for error type design are against including the source error in the display message, unless that source error is also not reachable in the source error chain. The context methods will put the error in the source chain, so including that error's message into the top level error's message is ill advised.
How do I get e returned by run?
See the chain method in order to traverse the chain of source errors and so enable you to peek into underlying causes. Still, anyhow's error type was mostly designed to be opaque. If you need better introspection into what the error entails or easier pattern matching on errors, consider using a structural error type instead of anyhow::Error (snafu or thiserror can help you build them).
See also:
Should an Error with a source include that source in the Display output?
What is the difference between "context" and "with_context" in anyhow?
Simplier way to return custom error type with anyhow::Error?

Nestjs error's location in exception filters

I have a global filter for exceptions. I want to create a logger. But in the filter, I can't see in which line or file the error occurred. Is it possible to know about it in the filter?
I want to do this because I don't want to add a logger to every part of the project. Any idea would be appreciated.
Every error, including Nest's HttpException class and child classes, should have a .stack property that tells where the error was thrown, what line, and what happened to get to that point. You can log that and then grok it as necessary to get the information you want

What range should I use for custom error codes in node?

I'm using meteor and want to throw an error within a template with a custom error code. What values are already taken, and is this an acceptable way of distinguishing between custom errors? I'm only catching these errors within this template, so creating custom error classes seems like overkill.
Im not sure if the built in meteor error will meet your needs, but it provides a way to have clearly defined types of errors without the need to make new error classes.
the code could be something like this:
const ERROR_TYPE_A = 'error-type-a';
// code...
throw new Meteor.Error(ERROR_TYPE_A, "this can be a human readable string, which could be displayed to the user");
in the catching context:
if (err.error === ERROR_TYPE_A){
// switch on type...
}
you could then type check the exception against the constant.
hope that helps.

node: illegal access error - using es6 proxies

When I run my code with node 0.10.26, I'm getting an 'illegal access' error when using ES6 proxies. It doesn't happen with node 0.11.14
Any ideas how I can try to approach this? There's no stack trace.
I have a pretty convoluted proxy implementation, I've implemented the following methods:
get, set, has, hasOwn, delete, keys, enumerate, getOwnPropertyNames, getPropertyNames, getOwnPropertyDescriptor, getPropertyDescriptor
Is there a Proxy test suite set I can throw at it to see if I've implemented something incorrectly? Or any other way to see the source of the problem? I don't even know how to invoke half of the things I implemented :)
Any libraries that I can replace the Proxy object with? I think I saw one before but can't find it now.
EDIT: more details I forgot: It's not that there's no stack track, there's a stack trace from bluebird promise and it begins with Promise$_rejectPromises, which makes me think the error is related to this problem but I still don't know how find the source error with the problematic property.
So while looking for a Proxy replacement, I stumbled onto this thread, which says that this problem happens when something tries to use JSON.stringify() on the proxy.
I'm happy to say that implementing my own toJSON() method on the proxy object solved the problem.
Ahhh... so good to be back to 0.10.26

Subclassing Bluebird's OperationalError

I'm promisifying a 3rd party library that doesn't throw typed errors, but instead uses the "err" function callback to notify the caller of the error. In this case, the "error" that the library is reporting is just an anonymous JS object with a few well-defined properties.
Bluebird is wrapping this in an OperationalError, which is great - but it'd be even more handy if I could subclass OperationalError and provide my own well-defined Error type that I could define. For instance LibraryXOperationalError - in order to distinguish the errors from this library, from some other error, in some sort of global express error handling middleware.
Is this possible? I tried to come up with a solution using the "promisifier" concept, but haven't had success.
OperationalError is subclassable just like Error is, you can get a reference from Promise.OperationalError

Resources