@belgattitude/http-exception
Version:
Warning: has been moved to @httpx/exception. Please update.
46 lines (43 loc) • 1.48 kB
JavaScript
import { typeof as _typeof, objectSpread2 as _objectSpread2 } from '../../_virtual/_rollupPluginBabelHelpers.js';
import { isHttpException } from '../../typeguards/isHttpException.js';
import { isNativeError } from '../typeguard/isNativeError.js';
var convertToSerializable = function convertToSerializable(e) {
var _ref = e instanceof Error ? e : {
name: 'Error',
message: typeof e === 'string' ? e : "Can't serialize error at runtime. Received '".concat(_typeof(e), "'")
},
name = _ref.name,
message = _ref.message,
_ref$stack = _ref.stack,
stack = _ref$stack === void 0 ? null : _ref$stack,
_ref$cause = _ref.cause,
c = _ref$cause === void 0 ? null : _ref$cause;
var cause = c instanceof Error ? convertToSerializable(c) : null;
var common = _objectSpread2(_objectSpread2({
name: name,
message: message
}, stack ? {
stack: stack
} : {}), cause ? {
cause: cause
} : {});
if (isHttpException(e)) {
return _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
__type: 'HttpException'
}, common), {}, {
statusCode: e.statusCode
}, e.url ? {
url: e.url
} : {}), e.code ? {
code: e.code
} : {}), e.method ? {
method: e.method
} : {}), e.errorId ? {
errorId: e.errorId
} : {});
}
return _objectSpread2({
__type: isNativeError(e) ? 'NativeError' : 'NonNativeError'
}, common);
};
export { convertToSerializable };