rn-webim-chat
Version:
Webim chat wrapper for React-Native. Supports Android and iOS. Fixed issues for native platforms build that are present in the official package.
52 lines (43 loc) • 1.41 kB
text/typescript
import type { WebimNativeError } from './types';
export function parseNativeResponse<T>(response?: T): T | null {
return response || null;
}
export function isWebimError(err: any): err is WebimNativeError {
const errorFields = Object.keys(err);
return errorFields.includes('errorCode') && errorFields.includes('errorType');
}
export function processError(error: WebimNativeError) {
return new Error(error.errorCode);
}
export class WebimSubscription {
readonly remove: () => void;
constructor(remove: () => void) {
this.remove = remove;
}
}
/**
* Parse error object, map it into {@link WebimNativeError} and decide should be thrown or not.
*
* @param {*} err - A caught error-object on Promise-level.
* @param {boolean} [throwable=true] - Optional parameter to define throw immediately
* or take error result and handle by your-self.
*
* @return {WebimNativeError} In case of not throwable.
*
* @throws {WebimNativeError}
*/
export function webimErrorHandler(
err: any,
throwable: boolean = true
): WebimNativeError {
const errorBody: WebimNativeError = {
errorCode:
err?.userInfo?.errorCode || err.errorCode || err?.code || 'UNKNWON',
message: err?.userInfo?.message || err?.message || 'Unexpected error',
errorType: err?.userInfo?.errorType || err?.errorType || 'common',
};
if (throwable) {
throw errorBody;
}
return errorBody;
}