error-serializer
Version:
Convert errors to/from plain objects
74 lines (55 loc) • 1.5 kB
JavaScript
import isPlainObj from"is-plain-obj";
import normalizeException from"normalize-exception";
import{isErrorObject,safeListKeys}from"../check.js";
import{listProps,NON_ENUMERABLE_PROPS,SET_CORE_PROPS}from"../props.js";
import{applyTransformInstance}from"../transform.js";
import{createError}from"./create.js";
export const parseDeep=(value,options)=>{
const valueA=parseRecurse(value,options);
return parseShallow(valueA,options)
};
export const parseShallow=(value,options)=>{
if(!isErrorObject(value)){
return value
}
const error=parseErrorObject(value,options);
const errorA=normalizeException(error);
applyTransformInstance(errorA,value,options);
return errorA
};
const parseErrorObject=(errorObject,options)=>{
const error=createError({errorObject,options});
setProps(error,errorObject);
return error
};
const setProps=(error,object)=>{
listProps(object).
filter(isNotCoreProp).
forEach((propName)=>{
setProp(error,object,propName)
})
};
const isNotCoreProp=(propName)=>!SET_CORE_PROPS.has(propName);
const setProp=(error,object,propName)=>{
const enumerable=!NON_ENUMERABLE_PROPS.has(propName);
Object.defineProperty(error,propName,{
value:object[propName],
enumerable,
writable:true,
configurable:true
})
};
const parseRecurse=(value,options)=>{
if(Array.isArray(value)){
return value.map((child)=>parseDeep(child,options))
}
if(isPlainObj(value)){
return Object.fromEntries(
safeListKeys(value).map((propName)=>[
propName,
parseDeep(value[propName],options)]
)
)
}
return value
};