Closed danielweinmann closed 2 years ago
We might even call it DomainFunctionError
Updated the screenshot on prev comment
To help with the TS, we'd need to ensure at least one property of ErrorData:
type AtLeastOne<T, U = {[K in keyof T]: Pick<T, K> }> = Partial<T> & U[keyof U]
class DomainFunctionError extends Error {
data: AtLeastOne<ErrorData>
constructor(data: AtLeastOne<ErrorData>) {
super(
data.errors[0]?.message
?? data.environmentErrors[0]?.message
?? data.inputErrors[0]?.message
)
this.name = "DomainFunctionError"
this.data = data
}
}
I know I'm missing the path
on the type above but just sketching a raw idea:
const domainFunction = makeDomainFunction(z.null())(async () => {
throw new DomainFunctionError({
errors: [{ message: 'Custom error' }],
inputErrors: [{ message: 'Custom input error', path: ['id'] }],
environmentErrors: [{ message: 'Custom environment error', path: ['id'] }],
})
})
Purpose
This PR allows domain functions to throw partial
ErrorData
to be able to returninputErrors
andenvironmentErrors
as well aserrors
. This is useful for implementing validations that go beyond the parsing of the schema, like "Email is already taken", for example.With this, whenever a server-side validation fails, we can simply throw the appropriate input errors, environment errors, or global errors.
What do you guys think about this solution?