Skip to content

Commit

Permalink
refactor(isValidLevels): new function in utils to remove direct use o…
Browse files Browse the repository at this point in the history
…f VALID_LEVELS
  • Loading branch information
eudaimos committed Apr 15, 2021
1 parent 9709492 commit 61877d5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/error-boundary.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import invariant from 'tiny-invariant';
import VALID_LEVELS, { LEVEL_ERROR } from './constants';
import { LEVEL_ERROR } from './constants';
import { Context, getRollbarFromContext } from './provider';
import * as utils from './utils';

const INITIAL_ERROR_STATE = { hasError: false, error: null };

Expand All @@ -23,7 +24,7 @@ export class ErrorBoundary extends Component {

constructor(props) {
super(props);
invariant(VALID_LEVELS.includes(props.level), `${props.level} is not a valid level setting for Rollbar`);
invariant(utils.isValidLevel(props.level), `${props.level} is not a valid level setting for Rollbar`);
this.state = { ...INITIAL_ERROR_STATE };
}

Expand Down
5 changes: 3 additions & 2 deletions src/hooks/use-rollbar-capture-event.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import invariant from 'tiny-invariant';
import VALID_LEVELS, { LEVEL_DEBUG, LEVEL_INFO, LEVEL_CRITICAL } from '../constants';
import { LEVEL_INFO } from '../constants';
import { useRollbar } from './use-rollbar';
import { isValidLevel } from '../utils';

export function useRollbarCaptureEvent(metadata, level = LEVEL_INFO) {
invariant(VALID_LEVELS[level] <= LEVEL_CRITICAL && VALID_LEVELS[level] >= LEVEL_DEBUG, '')
invariant(isValidLevel(level), `${level} is not a valid level setting for Rollbar`);
const rollbar = useRollbar();
useEffect(() => {
rollbar.captureEvent(metadata, level);
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/use-rollbar-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import { useEffect, useLayoutEffect } from 'react';
import invariant from 'tiny-invariant';
import VALID_LEVELS, { LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR, LEVEL_CRITICAL } from '../constants';
import { LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR, LEVEL_CRITICAL } from '../constants';
import { useRollbar } from './use-rollbar';
import { isValidLevel } from '../utils';

const LOG = 'log';

function useRollbarNotify(type, isLayout, ...args) {
invariant(type === LOG || VALID_LEVELS[type] >= LEVEL_DEBUG && VALID_LEVELS[type] <= LEVEL_CRITICAL, `cannot notify rollbar using method '${type}'`);
invariant(type === LOG || isValidLevel(type), `cannot notify rollbar using method '${type}'`);
const rollbar = useRollbar();
(isLayout ? useLayoutEffect : useEffect)(() => {
rollbar[type](...args);
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import VALID_LEVELS, { LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR, LEVEL_CRITICAL } from './constants';
export { VALID_LEVELS, LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR, LEVEL_CRITICAL }
export { LEVEL_DEBUG, LEVEL_INFO, LEVEL_WARN, LEVEL_ERROR, LEVEL_CRITICAL } from './constants';
export { historyContext } from './history-context';
export { Provider } from './provider';
export { ErrorBoundary } from './error-boundary';
export { RollbarContext } from './rollbar-context';
export { isValidLevel } from './utils';
export * from './hooks';
8 changes: 8 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as constants from './constants';

const VALID_LEVELS = constants.default;

export function value(val, defaultTo, ...args) {
if (typeof val === 'function') {
return val(...args);
Expand All @@ -8,3 +12,7 @@ export function value(val, defaultTo, ...args) {
export function wrapValue(val, defaultAs) {
return (defaultTo, ...args) => value(val, defaultAs === undefined ? defaultTo : defaultAs, ...args);
}

export function isValidLevel(level) {
return VALID_LEVELS[level] >= constants.LEVEL_DEBUG && VALID_LEVELS[level] <= constants.LEVEL_CRITICAL;
}

0 comments on commit 61877d5

Please sign in to comment.