// @flow /* eslint-disable no-console, no-plusplus, no-param-reassign */ import { Sentry, SentrySeverity } from 'react-native-sentry'; type Level = 'debug' | 'warning' | 'error' | 'info'; function log(message: string, ...args: Array): void { if (__DEV__) { console.log(message, ...args); } else { let _message = message; if (typeof _message !== 'string') { try { _message = JSON.stringify(message); } catch (err) { _message = String(message); } } breadcrumb(_message, 'debug'); } } /** * Manda um breadcrumb para o sentry */ function breadcrumb(message: string, category: string, level: Level = 'debug'): void { if (!__DEV__) { if (typeof message !== 'string') return; Sentry.captureBreadcrumb({ message, category, level, }); } } /** * Exibe um warn em desenvolvimento e manda para o sentry em produção */ function warn(title: string, ...args: Array): void { if (__DEV__) { console.warn(...args); } else if (typeof title === 'string') { Sentry.captureMessage(title, { level: SentrySeverity.Warning, extra: { args: JSON.stringify(args, null, 2), }, }); } } function warnLocal(...args: Array): void { if (__DEV__) { console.warn(...args); } } function error(err: Error): void { if (__DEV__) { if (err instanceof Error) { // $FlowFixMe err.framesToPop = 1; } console.error(err); } else if (err instanceof Error) { // $FlowFixMe err.framesToPop = 1; Sentry.captureException(err); } else if (typeof err === 'string') { Sentry.captureMessage(err); } } function requestError(err: any, message?: ?string): void { if (err.config) { if (__DEV__) { if (message && typeof message === 'string') { const newError = new Error(message); error(newError); } else { error(err); } } else { breadcrumb(`[HTTP_ERROR] URL:${err.config.url}`, 'http', 'error'); breadcrumb(`[HTTP_ERROR] METHOD:${err.config.method}`, 'http', 'error'); breadcrumb(`[HTTP_ERROR] DATA:${JSON.stringify(err.config.data)}`, 'http', 'error'); if (err.response && err.response.data) { breadcrumb( `[HTTP_ERROR] RESPONSE:${JSON.stringify(err.response.data)}`, 'http', 'error', ); } if (message && typeof message === 'string') { const newError = new Error(message); error(newError); } else { error(err); } } } else { error(err); } } /** * Verifica se uma condição é verdadeira, caso contrario lança uma exceção * @example * ```javascript * logger.assert(false, 'Sempre vai lançar uma exceção: %s', 'Nome da Exceção') * ``` */ function assert(condition: boolean, format: string, ...args: Array) { if (__DEV__) { if (condition) return; if (typeof format !== 'string') { throw new Error('"logger.assert" needs a format string'); } let argIndex = 0; const newError = new Error(format.replace(/%s/g, () => String(args[argIndex++]))); newError.name = 'AssertError'; // $FlowFixMe newError.framesToPop = 1; throw newError; } } export default { log, error, warn, assert, warnLocal, breadcrumb, requestError, };