throw custom ValidationError

contains an message formatter, displaying all messages concatinated.
This commit is contained in:
Thomas Ruoff
2020-09-15 23:38:13 +02:00
parent e3a9da6efa
commit 00d30a4459
2 changed files with 27 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
interface ValidationErrors {
[key: string]: { properties: { message: string } }
}
export class ValidationError extends Error {
private errors: ValidationErrors
constructor(errors: ValidationErrors) {
super()
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ValidationError)
}
this.name = this.constructor.name
this.errors = errors
}
get message() {
return Object.entries<{ properties: { message: string } }>(this.errors)
.map(([_key, value]) => {
return `${value?.properties?.message}`
})
.join(',')
}
}