UNPKG

error-serializer

Version:
51 lines (31 loc) 1.16 kB
import isErrorInstance from"is-error-instance"; import normalizeException from"normalize-exception"; import{normalizeOptions,validateOptions}from"./options.js"; import{parseDeep,parseShallow}from"./parse/main.js"; import{serializeDeep,serializeShallow}from"./serialize.js"; import{applyTransformInstance}from"./transform.js"; export{validateOptions}; export const serialize=(value,options)=>{ const{loose,shallow,...otherOptions}=normalizeOptions(options); const valueA=applySerializeLoose(value,loose); return shallow? serializeShallow(valueA,otherOptions): serializeDeep(valueA,otherOptions,[]) }; const applySerializeLoose=(value,loose)=> loose||isErrorInstance(value)?value:normalizeException(value); export const parse=(value,options)=>{ const{loose,shallow,...otherOptions}=normalizeOptions(options); const valueA=shallow? parseShallow(value,otherOptions): parseDeep(value,otherOptions); return applyParseLoose(valueA,loose,otherOptions) }; const applyParseLoose=(value,loose,options)=>{ if(loose||isErrorInstance(value)){ return value } const error=normalizeException(value); applyTransformInstance(error,value,options); return error };