Skip to content

Commit

Permalink
feat(NabuError): log all nabu errors to segment
Browse files Browse the repository at this point in the history
  • Loading branch information
awiermanncasas-bc committed Jul 26, 2022
1 parent 54ce539 commit babeda1
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ErrorSource = 'CLIENT' | 'NABU' | 'UNKNOWN'

export type ClientErrorProperties = {
action?: string
category?: string[]
error: ErrorType
network_endpoint?: string
network_error_code?: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,14 @@ describe('NabuError()', () => {
expect(actions[0].title).toEqual('Go to enter amount')
expect(actions[1].title).toEqual('Blockchain')
})

it('Should include the error categories when available', () => {
const error = new NabuError({
categories: ['TestCategory'],
message: 'Message',
title: 'Title'
})

expect(error.categories).toEqual(['TestCategory'])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ class NabuError extends Error {

message: string

categories?: string[]

actions?: NabuErrorAction[]

constructor({ actions, icon, message, title }: NabuErrorProps) {
constructor({ actions, categories, icon, message, title }: NabuErrorProps) {
super(title)

this.title = title
this.message = message
this.categories = categories
this.icon = icon
this.actions = actions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type NabuErrorIconProps = {

type NabuErrorProps = {
actions?: NabuErrorAction[]
categories?: string[]
icon?: NabuErrorIconProps
message: string
title: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createNabuErrorAnalyticsInterceptor, NabuError } from 'services/errors'

describe('createNabuErrorAnalyticsInterceptor()', () => {
it('Should not handle error that is not nabu', async () => {
const dispatchSpy = jest.fn()
const error = new Error()

const interceptor = createNabuErrorAnalyticsInterceptor(dispatchSpy)

const promise = interceptor(error)

await expect(promise).rejects.toEqual(error)

expect(dispatchSpy).not.toHaveBeenCalled()
})

it('Should push the nabu error to the analytics service', async () => {
const dispatchSpy = jest.fn()
const error = new NabuError({
categories: ['TestCategory'],
message: 'Message',
title: 'Error title'
})

const interceptor = createNabuErrorAnalyticsInterceptor(dispatchSpy)

const promise = interceptor(error)

await expect(promise).rejects.toEqual(error)

expect(dispatchSpy).toHaveBeenCalledWith({
payload: {
key: 'Client Error',
properties: {
category: ['TestCategory'],
error: 'NABU_ERROR',
network_error_description: error.message,
network_error_type: 'NABU_ERROR',
source: 'NABU',
title: error.title
}
},
type: 'trackEvent'
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { actions } from 'data'
import { ClientErrorProperties } from 'data/analytics/types/errors'
import { Analytics } from 'data/types'

import { isNabuError } from '../isNabuError'
import { CreateNabuErrorAnalyticsInterceptorUtility } from './createNabuErrorAnalyticsInterceptor.types'

export const createNabuErrorAnalyticsInterceptor: CreateNabuErrorAnalyticsInterceptorUtility =
(dispatch) => (error) => {
if (isNabuError(error)) {
const clientError: ClientErrorProperties = {
category: error.categories,
error: 'NABU_ERROR',
network_error_description: error.message,
network_error_type: 'NABU_ERROR',
source: 'NABU',
title: error.title
}

dispatch(
actions.analytics.trackEvent({
key: Analytics.CLIENT_ERROR,
properties: clientError
})
)
}

return Promise.reject(error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Dispatch } from 'redux'

export type CreateNabuErrorAnalyticsInterceptorUtility = (
dispatch: Dispatch
) => (error: Error) => Promise<Error>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './createNabuErrorAnalyticsInterceptor'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './createNabuErrorAnalyticsInterceptor'
export * from './createNabuErrorFulfilledInterceptor'
export * from './createNabuErrorRejectedInterceptor'
export * from './isNabuError'
Expand Down
9 changes: 7 additions & 2 deletions packages/blockchain-wallet-v4-frontend/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ApiSocket, createWalletApi, HorizonStreamingService, Socket } from '@co
import { serializer } from '@core/types'
import { actions, rootReducer, rootSaga, selectors } from 'data'
import { isBrowserSupported } from 'services/browser'
import { createNabuErrorFulfilledInterceptor, createNabuErrorRejectedInterceptor } from "services/errors/NabuError"
import { createNabuErrorFulfilledInterceptor, createNabuErrorRejectedInterceptor, createNabuErrorAnalyticsInterceptor } from "services/errors/NabuError"
import axios from "axios"

import {
Expand Down Expand Up @@ -103,7 +103,7 @@ const configuredStore = async function () {
axios.interceptors.response.use(
createNabuErrorFulfilledInterceptor(),
createNabuErrorRejectedInterceptor()
);
)

const api = createWalletApi({
apiKey: '1770d5d9-bcea-4d28-ad21-6cbd5be018a8',
Expand Down Expand Up @@ -162,6 +162,11 @@ const configuredStore = async function () {

store.dispatch(actions.goals.defineGoals())

axios.interceptors.response.use(
(response) => response,
createNabuErrorAnalyticsInterceptor(store.dispatch)
)

return {
history,
persistor,
Expand Down

0 comments on commit babeda1

Please sign in to comment.