theta-client-react-native
Version:
This library provides a way to control RICOH THETA using.
54 lines (53 loc) • 2.22 kB
JavaScript
import { toNumber, toStringValue } from './libs/convert-utils';
export 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;
}
}
/**
* 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 = toStringValue(nativeError.message) ?? (error instanceof Error ? error.message : String(error));
const statusCode = toNumber((_nativeError$userInfo = nativeError.userInfo) === null || _nativeError$userInfo === void 0 ? void 0 : _nativeError$userInfo.statusCode);
const errorCode = 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.
*/
export 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