-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What could a new tiny-invariant API look like? (Custom errors, prefixes, aliases etc) #169
Comments
Here is my favourite option I have come up with so far: // basic usage
invariant(condition);
// With a basic message
//(maybe it's time we drop the built-in "Invariant failed: " prefix for minimum bundles too)
invariant(condition, message)
// advanced (breaking change)
invariant(condition, () => new Error('My expensive custom message'));
invariant(condition, () => new MyCustomError('Error message'));
invariant(condition, () => Promise.resolve(4)); API changes: - `invariant(condition, string | () => string)`
+ `invariant(condition, string | () => unknown)` The second argument is currently used to compute expensive error messages. I think the second argument could be used as a getter for the whole value that is going to be thrown. This gives consumers heaps of control. Stripping error messagesIdeally, we remove // author
invariant(condition, message);
// production build
invariant(condition); This becomes more tricky when the second argument to Without additional tooling support, consumers would need to do something like this: invariant(condition, () => MyCustomError(
process.env.NODE_ENV !== 'production' ? 'My error message': ''
) Which is not great. It would be fantastic to get other peoples thoughts on this idea, as well as other ideas as well! Please do not feel limited by my suggestion |
Something I think about: what is the value of if(!condition) {
throw new MyCustomError(
process.env.NODE_ENV !== 'production' ? 'My error message': ''
)
} I find invariant(condition1);
invariant(condition2);
invariant(condition3); |
I'm using invariant do the form validate. if some input not match the requirement, I would like to show the message. try {
invariant(validateForm(), 'readable message to tell user what is wrong')
} catch (e) {
toast(e.message)
} But in production, the message got striped. |
I think I would rather have a "treeShakeMessageInProd" utility to use w/ I use |
Any updates on this? I would love to omit the prefix and throw custom errors. I wouldn't worry too much about the production build, as omitting the message can be done as described in the readme. |
Is it really a good idea to assume that everyone will use the string literal |
A new API may suit one use use case but not another. I created flexible-invariant with an invariant factory function to hopefully suit all of them. Here's an example of how I use my lib to create an invariant that throws export const invariant404: (
condition: any,
message?: string,
) => asserts condition = invariantFactory(
(message?: string) => new Response(message, { status: 404 }),
) |
A big goal of this library is to be as lite as possible. Yet, there are some common and useful features that would be great for this library to have:
message
can be astring
or lazy (eg with afunction
for when your error message is expensive to compute)Error
that is thrown should be able to be any Error (egMyCustomError
) or perhaps even any value (you might want to throw a promise for example) CusomError #166"Invariant failed: "
prefix should be optional (or perhaps removed completely - do we even need a prefix?) Option to return error without the prefix #143invariant
andassert
)assert
named export (More meaningful name forinvariant
function #153)All of these features are possible. I am trying to think through:
tiny-invariant
is to be tiny)The text was updated successfully, but these errors were encountered: