Skip to main content

Error Handling

All throwing functions in v3 raise a DrUtilsError instead of a plain Error. You can import it from dr-utils/errors and match on its .code field for structured handling.

import { parseCedula } from 'dr-utils'
import { DrUtilsError } from 'dr-utils/errors'

try {
parseCedula('invalid')
} catch (e) {
if (e instanceof DrUtilsError) {
console.error(e.code) // 'INVALID_CEDULA'
console.error(e.message) // '"invalid" is not a valid cedula.'
}
}

DrUtilsError extends Error, so existing instanceof Error checks still work.

Error codes

CodeThrown by
INVALID_CEDULAparseCedula, formatCedula, maskCedula
INVALID_RNCparseRNC, formatRNC, maskRNC
INVALID_NCFparseNCF, formatNCF
INVALID_PHONEparsePhoneNumber, formatPhoneNumber, maskPhoneNumber
INVALID_PLATEparsePlate, formatPlate
FORMAT_CEDULA_FAILEDformatCedula
FORMAT_RNC_FAILEDformatRNC
FORMAT_NCF_FAILEDformatNCF
FORMAT_PHONE_FAILEDformatPhoneNumber
FORMAT_PLATE_FAILEDformatPlate
ITBIS_NEGATIVEapplyItbis, removeItbis, splitItbis
AMOUNT_TO_WORDS_OUT_OF_RANGEamountToWords

Using safe parsers to avoid throws

If you prefer not to use try/catch, use the safe parser variants which return { ok: true, value } | { ok: false, error }.