Skip to content

Commit a1682c2

Browse files
committed
wip
1 parent 2690a3f commit a1682c2

File tree

6 files changed

+91
-4
lines changed

6 files changed

+91
-4
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { ErrorDetailsModal } from '@mongodb-js/compass-components';
3+
import { connect } from 'react-redux';
4+
import { RootImportState } from '../stores/import-store';
5+
import { onErrorDetailsClose } from '../modules/import';
6+
7+
const ImportErrorDetailsModal: React.FunctionComponent<{
8+
isOpen: boolean;
9+
errorDetails?: Record<string, unknown>;
10+
onClose: () => void;
11+
}> = ({ isOpen, errorDetails, onClose }) => {
12+
return (
13+
<ErrorDetailsModal
14+
closeAction="close"
15+
open={isOpen}
16+
details={errorDetails}
17+
onClose={onClose}
18+
/>
19+
);
20+
};
21+
22+
const ConnectedImportErrorDetailsModal = connect(
23+
(state: RootImportState) => ({
24+
isOpen: state.import.errorDetails.isOpen,
25+
errorDetails: state.import.errorDetails.details,
26+
}),
27+
{
28+
onClose: onErrorDetailsClose,
29+
}
30+
)(ImportErrorDetailsModal);
31+
32+
export default ConnectedImportErrorDetailsModal;

packages/compass-import-export/src/components/import-toast.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import React from 'react';
22
import {
33
Body,
4+
Button,
45
css,
56
openToast,
67
ToastBody,
78
} from '@mongodb-js/compass-components';
89
import path from 'path';
10+
import type { MongoServerError } from 'mongodb';
911

1012
const importToastId = 'import-toast';
1113
const bloatedDocumentSignalToastId = 'import-toast-bloated-document';
@@ -221,10 +223,23 @@ export function showCancelledToast({
221223
});
222224
}
223225

224-
export function showFailedToast(err: Error | undefined) {
226+
export function showFailedToast(
227+
err: Error | undefined,
228+
showErrorDetails?: () => void
229+
) {
230+
console.log({ err, showErrorDetails });
225231
openToast(importToastId, {
226232
title: 'Failed to import with the following error:',
227233
description: err?.message,
228234
variant: 'warning',
235+
actionElement: showErrorDetails && (err as MongoServerError).errInfo && (
236+
<Button
237+
size="xsmall"
238+
onClick={showErrorDetails}
239+
data-testid="insert-document-error-details-button"
240+
>
241+
VIEW ERROR DETAILS
242+
</Button>
243+
),
229244
});
230245
}

packages/compass-import-export/src/import-plugin.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import React from 'react';
22
import ImportModal from './components/import-modal';
33
import ImportInProgressModal from './components/import-in-progress-modal';
4+
import ImportErrorDetailsModal from './components/error-details-modal';
45

56
function ImportPlugin() {
67
return (
78
<>
89
<ImportModal />
910
<ImportInProgressModal />
11+
<ImportErrorDetailsModal />
1012
</>
1113
);
1214
}

packages/compass-import-export/src/import/import-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type ErrorJSON = {
2828
index?: number;
2929
code?: string | number;
3030
op?: any;
31-
errorInfo?: Document;
31+
errInfo?: Document;
3232
numErrors?: number;
3333
};
3434

packages/compass-import-export/src/import/import-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function errorToJSON(error: any): ErrorJSON {
4040
message: error.message,
4141
};
4242

43-
for (const key of ['index', 'code', 'op', 'errorInfo'] as const) {
43+
for (const key of ['index', 'code', 'op', 'errInfo'] as const) {
4444
if (error[key] !== undefined) {
4545
obj[key] = error[key];
4646
}

packages/compass-import-export/src/modules/import.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export const STARTED = `${PREFIX}/STARTED`;
4747
export const CANCELED = `${PREFIX}/CANCELED`;
4848
export const FINISHED = `${PREFIX}/FINISHED`;
4949
export const FAILED = `${PREFIX}/FAILED`;
50+
export const ERROR_DETAILS_OPENED = `${PREFIX}/ERROR_DETAILS_OPENED`;
51+
export const ERROR_DETAILS_CLOSED = `${PREFIX}/ERROR_DETAILS_CLOSED`;
5052
export const FILE_TYPE_SELECTED = `${PREFIX}/FILE_TYPE_SELECTED`;
5153
export const FILE_SELECTED = `${PREFIX}/FILE_SELECTED`;
5254
export const FILE_SELECT_ERROR = `${PREFIX}/FILE_SELECT_ERROR`;
@@ -80,13 +82,24 @@ type FieldFromJSON = {
8082
};
8183
type FieldType = FieldFromJSON | FieldFromCSV;
8284

85+
export type ErrorDetailsDialogState =
86+
| {
87+
isOpen: false;
88+
details?: Record<string, unknown>;
89+
}
90+
| {
91+
isOpen: true;
92+
details: Record<string, unknown>;
93+
};
94+
8395
type ImportState = {
8496
isOpen: boolean;
8597
isInProgressMessageOpen: boolean;
8698
firstErrors: Error[];
8799
fileType: AcceptedFileType | '';
88100
fileName: string;
89101
errorLogFilePath: string;
102+
errorDetails: ErrorDetailsDialogState;
90103
fileIsMultilineJSON: boolean;
91104
useHeaderLines: boolean;
92105
status: ProcessStatus;
@@ -122,6 +135,7 @@ export const INITIAL_STATE: ImportState = {
122135
firstErrors: [],
123136
fileName: '',
124137
errorLogFilePath: '',
138+
errorDetails: { isOpen: false },
125139
fileIsMultilineJSON: false,
126140
useHeaderLines: true,
127141
status: PROCESS_STATUS.UNSPECIFIED,
@@ -169,6 +183,8 @@ const onFinished = ({
169183

170184
const onFailed = (error: Error) => ({ type: FAILED, error });
171185

186+
export const onErrorDetailsClose = () => ({ type: ERROR_DETAILS_CLOSED });
187+
172188
const onFileSelectError = (error: Error) => ({
173189
type: FILE_SELECT_ERROR,
174190
error,
@@ -373,7 +389,9 @@ export const startImport = (): ImportThunkAction<Promise<void>> => {
373389
debug('Error while importing:', err.stack);
374390

375391
progressCallback.flush();
376-
showFailedToast(err);
392+
showFailedToast(err, () =>
393+
dispatch({ type: ERROR_DETAILS_OPENED, errorDetails: err.errInfo })
394+
);
377395

378396
dispatch(onFailed(err));
379397
return;
@@ -1069,6 +1087,26 @@ export const importReducer: Reducer<ImportState> = (
10691087
};
10701088
}
10711089

1090+
if (action.type === ERROR_DETAILS_OPENED) {
1091+
return {
1092+
...state,
1093+
errorDetails: {
1094+
isOpen: true,
1095+
details: action.errorDetails,
1096+
},
1097+
};
1098+
}
1099+
1100+
if (action.type === ERROR_DETAILS_CLOSED) {
1101+
return {
1102+
...state,
1103+
errorDetails: {
1104+
...state.errorDetails,
1105+
isOpen: false,
1106+
},
1107+
};
1108+
}
1109+
10721110
if (action.type === STARTED) {
10731111
return {
10741112
...state,

0 commit comments

Comments
 (0)