Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ import { createContext } from 'react';
* @property {Payload=} payload Additional payload arguments to log with action.
*/

/**
* @typedef {(error: Error)=>void} NoticeError
*/

/**
* @typedef AnalyticsContext
*
* @prop {TrackEvent} addPageAction Log an action with optional payload.
* @prop {NoticeError} noticeError Log an error without affecting application behavior.
*/

const AnalyticsContext = createContext(
/** @type {AnalyticsContext} */ ({
addPageAction: () => Promise.resolve(),
noticeError: () => {},
}),
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useContext } from 'react';
import { t } from '@18f/identity-i18n';
import { FormError } from '@18f/identity-form-steps';
import { trackError } from '@18f/identity-analytics';
import UploadContext from '../context/upload';
import AnalyticsContext from '../context/analytics';

Expand Down Expand Up @@ -83,7 +84,7 @@ const withBackgroundEncryptedUpload = (Component) => {
*/
function ComposedComponent({ onChange, onError, ...props }) {
const { backgroundUploadURLs, backgroundUploadEncryptKey } = useContext(UploadContext);
const { addPageAction, noticeError } = useContext(AnalyticsContext);
const { addPageAction } = useContext(AnalyticsContext);

/**
* @param {Record<string, string|Blob|null|undefined>} nextValues Next values.
Expand All @@ -103,7 +104,7 @@ const withBackgroundEncryptedUpload = (Component) => {
)
.catch((error) => {
addPageAction('IdV: document capture async upload encryption', { success: false });
noticeError(error);
trackError(error);

// Rethrow error to skip upload and proceed from next `catch` block.
throw error;
Expand Down
20 changes: 2 additions & 18 deletions app/javascript/packs/document-capture.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,20 @@ import { trackEvent } from '@18f/identity-analytics';
/** @typedef {import('@18f/identity-document-capture').FlowPath} FlowPath */
/** @typedef {import('@18f/identity-i18n').I18n} I18n */

/**
* @typedef NewRelicAgent
*
* @prop {(error:Error)=>void} noticeError Log an error without affecting application behavior.
*/

/**
* @typedef LoginGov
*
* @prop {Record<string,string>} assets
*/

/**
* @typedef NewRelicGlobals
*
* @prop {NewRelicAgent=} newrelic New Relic agent.
*/

/**
* @typedef LoginGovGlobals
*
* @prop {LoginGov} LoginGov
*/

/**
* @typedef {typeof window & NewRelicGlobals & LoginGovGlobals} DocumentCaptureGlobal
* @typedef {typeof window & LoginGovGlobals} DocumentCaptureGlobal
*/

/**
Expand Down Expand Up @@ -109,10 +97,6 @@ function addPageAction(event, payload) {
return trackEvent(event, { ...payload, flow_path: flowPath });
}

/** @type {import('@18f/identity-document-capture/context/analytics').NoticeError} */
const noticeError = (error) =>
/** @type {DocumentCaptureGlobal} */ (window).newrelic?.noticeError(error);

(async () => {
const backgroundUploadURLs = getBackgroundUploadURLs();
const isAsyncForm = Object.keys(backgroundUploadURLs).length > 0;
Expand Down Expand Up @@ -159,7 +143,7 @@ const noticeError = (error) =>
[AppContext.Provider, { value: { appName } }],
[HelpCenterContextProvider, { value: { helpCenterRedirectURL } }],
[DeviceContext.Provider, { value: device }],
[AnalyticsContext.Provider, { value: { addPageAction, noticeError } }],
[AnalyticsContext.Provider, { value: { addPageAction } }],
[
AcuantContextProvider,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import withBackgroundEncryptedUpload, {
encrypt,
} from '@18f/identity-document-capture/higher-order/with-background-encrypted-upload';
import { useSandbox } from '@18f/identity-test-helpers';
import * as analytics from '@18f/identity-analytics';
import { render } from '../../../support/document-capture';

/**
Expand Down Expand Up @@ -132,7 +133,6 @@ describe('document-capture/higher-order/with-background-encrypted-upload', () =>
describe('upload', () => {
async function renderWithResponse(response) {
const addPageAction = sinon.spy();
const noticeError = sinon.spy();
const onChange = sinon.spy();
const onError = sinon.spy();
const key = await window.crypto.subtle.generateKey(
Expand All @@ -145,7 +145,7 @@ describe('document-capture/higher-order/with-background-encrypted-upload', () =>
);
sandbox.stub(window, 'fetch').callsFake(() => Promise.resolve(response));
render(
<AnalyticsContext.Provider value={{ addPageAction, noticeError }}>
<AnalyticsContext.Provider value={{ addPageAction }}>
<UploadContextProvider
backgroundUploadURLs={{ foo: 'about:blank' }}
backgroundUploadEncryptKey={key}
Expand All @@ -155,7 +155,7 @@ describe('document-capture/higher-order/with-background-encrypted-upload', () =>
</AnalyticsContext.Provider>,
);

return { onChange, onError, addPageAction, noticeError };
return { onChange, onError, addPageAction };
}

context('success', () => {
Expand Down Expand Up @@ -237,9 +237,8 @@ describe('document-capture/higher-order/with-background-encrypted-upload', () =>
it('logs and throws on failed encryption', async () => {
const error = new Error();
sandbox.stub(window.crypto.subtle, 'encrypt').throws(error);
const { onChange, onError, addPageAction, noticeError } = await renderWithResponse(
response,
);
sandbox.spy(analytics, 'trackError');
const { onChange, onError, addPageAction } = await renderWithResponse(response);

const patch = onChange.getCall(0).args[0];
await patch.foo_image_url.catch(() => {});
Expand All @@ -251,7 +250,7 @@ describe('document-capture/higher-order/with-background-encrypted-upload', () =>
'IdV: document capture async upload encryption',
{ success: false },
);
expect(noticeError).to.have.been.calledWith(error);
expect(analytics.trackError).to.have.been.calledWith(error);
expect(window.fetch).not.to.have.been.called();
});

Expand Down