theta-client-react-native
Version:
This library provides a way to control RICOH THETA using.
62 lines (60 loc) • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ThetaWebApiError = void 0;
exports.normalizeNativeError = normalizeNativeError;
var _convertUtils = require("./libs/convert-utils");
class ThetaWebApiError extends Error {
constructor(message, statusCode, errorCode, e) {
super(message);
this.name = 'ThetaWebApiError';
this.statusCode = statusCode;
this.errorCode = errorCode;
this.moduleError = e instanceof Error ? e : undefined;
}
}
exports.ThetaWebApiError = ThetaWebApiError;
/**
* Extract THETA Web API error details from a native bridge error.
*
* Current bridge contract:
* - message: native error message (fallback to Error/String representation)
* - statusCode: nativeError.userInfo.statusCode
* - errorCode: nativeError.userInfo.errorCode
*
* Returns undefined when neither statusCode nor errorCode is available.
*/
function _extractThetaWebApiError(error) {
var _nativeError$userInfo, _nativeError$userInfo2;
if (error == null || typeof error !== 'object') {
return undefined;
}
const nativeError = error;
const message = (0, _convertUtils.toStringValue)(nativeError.message) ?? (error instanceof Error ? error.message : String(error));
const statusCode = (0, _convertUtils.toNumber)((_nativeError$userInfo = nativeError.userInfo) === null || _nativeError$userInfo === void 0 ? void 0 : _nativeError$userInfo.statusCode);
const errorCode = (0, _convertUtils.toStringValue)((_nativeError$userInfo2 = nativeError.userInfo) === null || _nativeError$userInfo2 === void 0 ? void 0 : _nativeError$userInfo2.errorCode);
if (statusCode == null && errorCode == null) {
return undefined;
}
return new ThetaWebApiError(message, statusCode, errorCode, error);
}
/**
* Normalize a native bridge error into Error with THETA Web API details.
*
* If THETA Web API details are not available, returns the original Error when
* possible, otherwise creates a generic Error.
* String errors are returned as-is.
*/
function normalizeNativeError(error) {
if (typeof error === 'string') {
return error;
}
const details = _extractThetaWebApiError(error);
if (details == null) {
return error instanceof Error ? error : new Error(String(error));
}
console.log(`[ThetaWebApiError] statusCode=${details.statusCode ?? '-'} errorCode=${details.errorCode ?? '-'} message=${details.message}`);
return details;
}
//# sourceMappingURL=theta-web-api-error.js.map