Prevent error name mangling during minification
mainMinifiers and transpilers can mangle custom error names, which causes issues in stack traces or when checking error names instead of using instanceof.
To ensure the error name remains correct, explicitly set the name property using Object.defineProperty in your constructor or factory function.
// For Class-based errors
import { CustomError } from 'ts-custom-error'
class MyError extends CustomError {
constructor() {
super()
Object.defineProperty(this, 'name', { value: 'MyError' })
}
}
// For Factory-based errors
import { customErrorFactory } from 'ts-custom-error'
const MyError = customErrorFactory(function MyError () {
Object.defineProperty(this, 'name', { value: 'MyError' })
})