balibabu commited on
Commit
8dff6d9
·
1 Parent(s): e090e58

fix: display specific error message when previewing file error #868 (#869)

Browse files

### What problem does this PR solve?

fix: display specific error message when previewing file error #868


### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

web/src/pages/document-viewer/docx/index.tsx CHANGED
@@ -5,7 +5,7 @@ import { useFetchDocx } from '../hooks';
5
  import styles from './index.less';
6
 
7
  const Docx = ({ filePath }: { filePath: string }) => {
8
- const { succeed, containerRef } = useFetchDocx(filePath);
9
 
10
  return (
11
  <>
@@ -16,7 +16,7 @@ const Docx = ({ filePath }: { filePath: string }) => {
16
  </div>
17
  </section>
18
  ) : (
19
- <FileError></FileError>
20
  )}
21
  </>
22
  );
 
5
  import styles from './index.less';
6
 
7
  const Docx = ({ filePath }: { filePath: string }) => {
8
+ const { succeed, containerRef, error } = useFetchDocx(filePath);
9
 
10
  return (
11
  <>
 
16
  </div>
17
  </section>
18
  ) : (
19
+ <FileError>{error}</FileError>
20
  )}
21
  </>
22
  );
web/src/pages/document-viewer/excel/index.tsx CHANGED
@@ -3,7 +3,7 @@ import FileError from '../file-error';
3
  import { useFetchExcel } from '../hooks';
4
 
5
  const Excel = ({ filePath }: { filePath: string }) => {
6
- const { status, containerRef } = useFetchExcel(filePath);
7
 
8
  return (
9
  <div
@@ -11,7 +11,7 @@ const Excel = ({ filePath }: { filePath: string }) => {
11
  ref={containerRef}
12
  style={{ height: '100%', width: '100%' }}
13
  >
14
- {status || <FileError></FileError>}
15
  </div>
16
  );
17
  };
 
3
  import { useFetchExcel } from '../hooks';
4
 
5
  const Excel = ({ filePath }: { filePath: string }) => {
6
+ const { status, containerRef, error } = useFetchExcel(filePath);
7
 
8
  return (
9
  <div
 
11
  ref={containerRef}
12
  style={{ height: '100%', width: '100%' }}
13
  >
14
+ {status || <FileError>{error}</FileError>}
15
  </div>
16
  );
17
  };
web/src/pages/document-viewer/hooks.ts CHANGED
@@ -3,18 +3,38 @@ import axios from 'axios';
3
  import mammoth from 'mammoth';
4
  import { useCallback, useEffect, useRef, useState } from 'react';
5
 
6
- const useFetchDocument = () => {
7
- const fetchDocument = useCallback((api: string) => {
8
- return axios.get(api, { responseType: 'arraybuffer' });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  }, []);
10
 
11
- return fetchDocument;
12
  };
13
 
14
  export const useFetchExcel = (filePath: string) => {
15
  const [status, setStatus] = useState(true);
16
- const fetchDocument = useFetchDocument();
17
  const containerRef = useRef<HTMLDivElement>(null);
 
18
 
19
  const fetchDocumentAsync = useCallback(async () => {
20
  let myExcelPreviewer;
@@ -39,13 +59,14 @@ export const useFetchExcel = (filePath: string) => {
39
  fetchDocumentAsync();
40
  }, [fetchDocumentAsync]);
41
 
42
- return { status, containerRef };
43
  };
44
 
45
  export const useFetchDocx = (filePath: string) => {
46
  const [succeed, setSucceed] = useState(true);
47
- const fetchDocument = useFetchDocument();
48
  const containerRef = useRef<HTMLDivElement>(null);
 
49
 
50
  const fetchDocumentAsync = useCallback(async () => {
51
  const jsonFile = await fetchDocument(filePath);
@@ -64,9 +85,8 @@ export const useFetchDocx = (filePath: string) => {
64
  container.innerHTML = docEl.outerHTML;
65
  }
66
  })
67
- .catch((a) => {
68
  setSucceed(false);
69
- console.warn('alexei: something went wrong', a);
70
  });
71
  }, [filePath, fetchDocument]);
72
 
@@ -74,5 +94,5 @@ export const useFetchDocx = (filePath: string) => {
74
  fetchDocumentAsync();
75
  }, [fetchDocumentAsync]);
76
 
77
- return { succeed, containerRef };
78
  };
 
3
  import mammoth from 'mammoth';
4
  import { useCallback, useEffect, useRef, useState } from 'react';
5
 
6
+ export const useCatchError = (api: string) => {
7
+ const [error, setError] = useState('');
8
+ const fetchDocument = useCallback(async () => {
9
+ const ret = await axios.get(api);
10
+ const { data } = ret;
11
+ if (!(data instanceof ArrayBuffer) && data.retcode !== 0) {
12
+ setError(data.retmsg);
13
+ }
14
+ return ret;
15
+ }, [api]);
16
+
17
+ useEffect(() => {
18
+ fetchDocument();
19
+ }, [fetchDocument]);
20
+
21
+ return { fetchDocument, error };
22
+ };
23
+
24
+ export const useFetchDocument = () => {
25
+ const fetchDocument = useCallback(async (api: string) => {
26
+ const ret = await axios.get(api, { responseType: 'arraybuffer' });
27
+ return ret;
28
  }, []);
29
 
30
+ return { fetchDocument };
31
  };
32
 
33
  export const useFetchExcel = (filePath: string) => {
34
  const [status, setStatus] = useState(true);
35
+ const { fetchDocument } = useFetchDocument();
36
  const containerRef = useRef<HTMLDivElement>(null);
37
+ const { error } = useCatchError(filePath);
38
 
39
  const fetchDocumentAsync = useCallback(async () => {
40
  let myExcelPreviewer;
 
59
  fetchDocumentAsync();
60
  }, [fetchDocumentAsync]);
61
 
62
+ return { status, containerRef, error };
63
  };
64
 
65
  export const useFetchDocx = (filePath: string) => {
66
  const [succeed, setSucceed] = useState(true);
67
+ const { fetchDocument } = useFetchDocument();
68
  const containerRef = useRef<HTMLDivElement>(null);
69
+ const { error } = useCatchError(filePath);
70
 
71
  const fetchDocumentAsync = useCallback(async () => {
72
  const jsonFile = await fetchDocument(filePath);
 
85
  container.innerHTML = docEl.outerHTML;
86
  }
87
  })
88
+ .catch(() => {
89
  setSucceed(false);
 
90
  });
91
  }, [filePath, fetchDocument]);
92
 
 
94
  fetchDocumentAsync();
95
  }, [fetchDocumentAsync]);
96
 
97
+ return { succeed, containerRef, error };
98
  };
web/src/pages/document-viewer/pdf/index.tsx CHANGED
@@ -1,12 +1,14 @@
1
  import { Skeleton } from 'antd';
2
  import { PdfHighlighter, PdfLoader } from 'react-pdf-highlighter';
3
  import FileError from '../file-error';
 
4
 
5
  interface IProps {
6
  url: string;
7
  }
8
 
9
- const DocumentPreviewer = ({ url }: IProps) => {
 
10
  const resetHash = () => {};
11
 
12
  return (
@@ -15,7 +17,7 @@ const DocumentPreviewer = ({ url }: IProps) => {
15
  url={url}
16
  beforeLoad={<Skeleton active />}
17
  workerSrc="/pdfjs-dist/pdf.worker.min.js"
18
- errorMessage={<FileError></FileError>}
19
  onError={(e) => {
20
  console.warn(e);
21
  }}
@@ -40,4 +42,4 @@ const DocumentPreviewer = ({ url }: IProps) => {
40
  );
41
  };
42
 
43
- export default DocumentPreviewer;
 
1
  import { Skeleton } from 'antd';
2
  import { PdfHighlighter, PdfLoader } from 'react-pdf-highlighter';
3
  import FileError from '../file-error';
4
+ import { useCatchError } from '../hooks';
5
 
6
  interface IProps {
7
  url: string;
8
  }
9
 
10
+ const PdfPreviewer = ({ url }: IProps) => {
11
+ const { error } = useCatchError(url);
12
  const resetHash = () => {};
13
 
14
  return (
 
17
  url={url}
18
  beforeLoad={<Skeleton active />}
19
  workerSrc="/pdfjs-dist/pdf.worker.min.js"
20
+ errorMessage={<FileError>{error}</FileError>}
21
  onError={(e) => {
22
  console.warn(e);
23
  }}
 
42
  );
43
  };
44
 
45
+ export default PdfPreviewer;