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
FORMAT_DOP_FAILEDformatDOP (non-finite value)
ITBIS_NEGATIVEapplyItbis, removeItbis, splitItbis
ITBIS_INVALID_AMOUNTapplyItbis, removeItbis, splitItbis (non-finite amount)
ITBIS_INVALID_RATEapplyItbis, removeItbis, splitItbis (negative or non-finite rate)
AMOUNT_TO_WORDS_OUT_OF_RANGEamountToWords (negative, non-finite, or over 999,999,999.99)
PAYROLL_INVALID_AMOUNTcalculateTSS, calculateISR, calculateNetSalary (negative or non-finite amount)
PAYROLL_INVALID_OPTIONcalculateTSS, calculateNetSalary (negative or non-finite minimumWage / srlRate)

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 }.