Skip to content

Commit

Permalink
Migrate from Error/errorOnDev/warnOnDev to tiny-invariant/tiny-warnin…
Browse files Browse the repository at this point in the history
…g, update messages (#925)
  • Loading branch information
wojtekmaj authored Jan 19, 2022
1 parent 229b6a0 commit 95cd4ef
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 102 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
"merge-class-names": "^1.1.1",
"merge-refs": "^1.0.0",
"pdfjs-dist": "2.10.377",
"prop-types": "^15.6.2"
"prop-types": "^15.6.2",
"tiny-invariant": "^1.0.0",
"tiny-warning": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.15.0",
Expand Down
27 changes: 16 additions & 11 deletions src/Document.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import PropTypes from 'prop-types';
import makeEventProps from 'make-event-props';
import makeCancellable from 'make-cancellable-promise';
import mergeClassNames from 'merge-class-names';
import invariant from 'tiny-invariant';
import warning from 'tiny-warning';
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf';

import DocumentContext from './DocumentContext';
Expand All @@ -19,14 +21,12 @@ import {
cancelRunningTask,
dataURItoByteString,
displayCORSWarning,
errorOnDev,
isArrayBuffer,
isBlob,
isBrowser,
isDataURI,
isFile,
loadFromFile,
warnOnDev,
} from './shared/utils';

import {
Expand Down Expand Up @@ -63,7 +63,10 @@ export default class Document extends PureComponent {
return;
}

warnOnDev(`Warning: An internal link leading to page ${pageNumber} was clicked, but neither <Document> was provided with onItemClick nor it was able to find the page within itself. Either provide onItemClick to <Document> and handle navigating by yourself or ensure that all pages are rendered within <Document>.`);
warning(
false,
`An internal link leading to page ${pageNumber} was clicked, but neither <Document> was provided with onItemClick nor it was able to find the page within itself. Either provide onItemClick to <Document> and handle navigating by yourself or ensure that all pages are rendered within <Document>.`,
);
},
};

Expand Down Expand Up @@ -192,7 +195,7 @@ export default class Document extends PureComponent {
* Called when a document source failed to be resolved correctly
*/
onSourceError = (error) => {
errorOnDev(error);
warning(error);

const { onSourceError } = this.props;

Expand All @@ -218,7 +221,7 @@ export default class Document extends PureComponent {
onLoadError = (error) => {
this.setState({ pdf: false });

errorOnDev(error);
warning(error);

const { onLoadError } = this.props;

Expand Down Expand Up @@ -271,13 +274,15 @@ export default class Document extends PureComponent {
}

// At this point, file must be an object
if (typeof file !== 'object') {
throw new Error('Invalid parameter in file, need either Uint8Array, string or a parameter object');
}
invariant(
typeof file === 'object',
'Invalid parameter in file, need either Uint8Array, string or a parameter object',
);

if (!file.url && !file.data && !file.range) {
throw new Error('Invalid parameter object: need either .data, .range or .url');
}
invariant(
file.url || file.data || file.range,
'Invalid parameter object: need either .data, .range or .url',
);

// File .url is a string
if (typeof file.url === 'string') {
Expand Down
16 changes: 8 additions & 8 deletions src/LinkService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import invariant from 'tiny-invariant';

/* eslint-disable class-methods-use-this, no-empty-function */

Expand Down Expand Up @@ -61,9 +62,7 @@ export default class LinkService {
}
})
.then((explicitDest) => {
if (!Array.isArray(explicitDest)) {
throw new Error(`"${explicitDest}" is not a valid destination array.`);
}
invariant(Array.isArray(explicitDest), `"${explicitDest}" is not a valid destination array.`);

const destRef = explicitDest[0];

Expand All @@ -74,20 +73,21 @@ export default class LinkService {
resolve(pageIndex);
})
.catch(() => {
throw new Error(`"${destRef}" is not a valid page reference.`);
invariant(false, `"${destRef}" is not a valid page reference.`);
});
} else if (typeof destRef === 'number') {
resolve(destRef);
} else {
throw new Error(`"${destRef}" is not a valid destination reference.`);
invariant(false, `"${destRef}" is not a valid destination reference.`);
}
})
.then((pageIndex) => {
const pageNumber = pageIndex + 1;

if (!pageNumber || pageNumber < 1 || pageNumber > this.pagesCount) {
throw new Error(`"${pageNumber}" is not a valid page number.`);
}
invariant(
pageNumber >= 1 && pageNumber <= this.pagesCount,
`"${pageNumber}" is not a valid page number.`,
);

this.pdfViewer.scrollPageIntoView({
dest,
Expand Down
13 changes: 5 additions & 8 deletions src/Outline.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import PropTypes from 'prop-types';
import makeCancellable from 'make-cancellable-promise';
import makeEventProps from 'make-event-props';
import mergeClassNames from 'merge-class-names';
import invariant from 'tiny-invariant';
import warning from 'tiny-warning';

import DocumentContext from './DocumentContext';
import OutlineContext from './OutlineContext';

import OutlineItem from './OutlineItem';

import {
cancelRunningTask,
errorOnDev,
} from './shared/utils';
import { cancelRunningTask } from './shared/utils';

import {
eventProps,
Expand All @@ -29,9 +28,7 @@ export class OutlineInternal extends PureComponent {
componentDidMount() {
const { pdf } = this.props;

if (!pdf) {
throw new Error('Attempted to load an outline, but no document was specified.');
}
invariant(pdf, 'Attempted to load an outline, but no document was specified.');

this.loadOutline();
}
Expand Down Expand Up @@ -97,7 +94,7 @@ export class OutlineInternal extends PureComponent {
onLoadError = (error) => {
this.setState({ outline: false });

errorOnDev(error);
warning(error);

const { onLoadError } = this.props;

Expand Down
12 changes: 6 additions & 6 deletions src/Page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import makeCancellable from 'make-cancellable-promise';
import makeEventProps from 'make-event-props';
import mergeClassNames from 'merge-class-names';
import mergeRefs from 'merge-refs';
import invariant from 'tiny-invariant';
import warning from 'tiny-warning';

import DocumentContext from './DocumentContext';
import PageContext from './PageContext';
Expand All @@ -16,7 +18,6 @@ import AnnotationLayer from './Page/AnnotationLayer';

import {
cancelRunningTask,
errorOnDev,
isProvided,
makePageCallback,
} from './shared/utils';
Expand All @@ -42,9 +43,7 @@ export class PageInternal extends PureComponent {
componentDidMount() {
const { pdf } = this.props;

if (!pdf) {
throw new Error('Attempted to load a page, but no document was specified.');
}
invariant(pdf, 'Attempted to load a page, but no document was specified.');

this.loadPage();
}
Expand Down Expand Up @@ -127,7 +126,9 @@ export class PageInternal extends PureComponent {
* Called when a page failed to load
*/
onLoadError = (error) => {
errorOnDev(error);
this.setState({ page: false });

warning(error);

const { onLoadError } = this.props;

Expand Down Expand Up @@ -256,7 +257,6 @@ export class PageInternal extends PureComponent {
this.setState({ page }, this.onLoadSuccess);
})
.catch((error) => {
this.setState({ page: false });
this.onLoadError(error);
});
}
Expand Down
17 changes: 7 additions & 10 deletions src/Page/AnnotationLayer.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf';
import makeCancellable from 'make-cancellable-promise';
import invariant from 'tiny-invariant';
import warning from 'tiny-warning';
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf';

import DocumentContext from '../DocumentContext';
import PageContext from '../PageContext';

import {
cancelRunningTask,
errorOnDev,
} from '../shared/utils';
import { cancelRunningTask } from '../shared/utils';

import { isLinkService, isPage, isRotate } from '../shared/propTypes';

Expand All @@ -21,9 +20,7 @@ export class AnnotationLayerInternal extends PureComponent {
componentDidMount() {
const { page } = this.props;

if (!page) {
throw new Error('Attempted to load page annotations, but no page was specified.');
}
invariant(page, 'Attempted to load page annotations, but no page was specified.');

this.loadAnnotations();
}
Expand Down Expand Up @@ -68,7 +65,7 @@ export class AnnotationLayerInternal extends PureComponent {
onLoadError = (error) => {
this.setState({ annotations: false });

errorOnDev(error);
warning(error);

const { onGetAnnotationsError } = this.props;

Expand All @@ -85,7 +82,7 @@ export class AnnotationLayerInternal extends PureComponent {
* Called when a annotations fails to render.
*/
onRenderError = (error) => {
errorOnDev(error);
warning(error);

const { onRenderAnnotationLayerError } = this.props;

Expand Down
4 changes: 2 additions & 2 deletions src/Page/PageCanvas.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import mergeRefs from 'merge-refs';
import warning from 'tiny-warning';

import PageContext from '../PageContext';

import {
errorOnDev,
getPixelRatio,
isCancelException,
makePageCallback,
Expand Down Expand Up @@ -70,7 +70,7 @@ export class PageCanvasInternal extends PureComponent {
return;
}

errorOnDev(error);
warning(error);

const { onRenderError } = this.props;

Expand Down
4 changes: 2 additions & 2 deletions src/Page/PageSVG.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import warning from 'tiny-warning';
import * as pdfjs from 'pdfjs-dist/legacy/build/pdf';

import PageContext from '../PageContext';

import {
errorOnDev,
isCancelException,
makePageCallback,
} from '../shared/utils';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class PageSVGInternal extends PureComponent {
return;
}

errorOnDev(error);
warning(error);

const { onRenderError } = this.props;

Expand Down
13 changes: 5 additions & 8 deletions src/Page/TextLayer.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import makeCancellable from 'make-cancellable-promise';
import invariant from 'tiny-invariant';
import warning from 'tiny-warning';

import PageContext from '../PageContext';

import TextLayerItem from './TextLayerItem';

import {
cancelRunningTask,
errorOnDev,
} from '../shared/utils';
import { cancelRunningTask } from '../shared/utils';

import { isPage, isRotate } from '../shared/propTypes';

Expand All @@ -21,9 +20,7 @@ export class TextLayerInternal extends PureComponent {
componentDidMount() {
const { page } = this.props;

if (!page) {
throw new Error('Attempted to load page text content, but no page was specified.');
}
invariant(page, 'Attempted to load page text content, but no page was specified.');

this.loadTextItems();
}
Expand Down Expand Up @@ -65,7 +62,7 @@ export class TextLayerInternal extends PureComponent {
onLoadError = (error) => {
this.setState({ textItems: false });

errorOnDev(error);
warning(error);

const { onGetTextError } = this.props;

Expand Down
6 changes: 2 additions & 4 deletions src/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import Document from './Document';
import Outline from './Outline';
import Page from './Page';

import { isLocalFileSystem, warnOnDev } from './shared/utils';
import { displayWorkerWarning } from './shared/utils';

if (isLocalFileSystem) {
warnOnDev('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.');
}
displayWorkerWarning();

pdfjs.GlobalWorkerOptions.workerSrc = 'pdf.worker.js';

Expand Down
6 changes: 2 additions & 4 deletions src/entry.parcel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import Document from './Document';
import Outline from './Outline';
import Page from './Page';

import { isLocalFileSystem, warnOnDev } from './shared/utils';
import { displayWorkerWarning } from './shared/utils';

if (isLocalFileSystem) {
warnOnDev('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.');
}
displayWorkerWarning();

if (typeof window !== 'undefined' && 'Worker' in window) {
pdfjs.GlobalWorkerOptions.workerPort = new Worker('./pdf.worker.entry.js');
Expand Down
6 changes: 2 additions & 4 deletions src/entry.parcel2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import Document from './Document';
import Outline from './Outline';
import Page from './Page';

import { isLocalFileSystem, warnOnDev } from './shared/utils';
import { displayWorkerWarning } from './shared/utils';

if (isLocalFileSystem) {
warnOnDev('You are running React-PDF from your local file system. PDF.js Worker may fail to load due to browser\'s security policies. If you\'re on Google Chrome, you can use --allow-file-access-from-files flag for debugging purposes.');
}
displayWorkerWarning();

pdfjs.GlobalWorkerOptions.workerSrc = new URL('npm:pdfjs-dist/legacy/build/pdf.worker.entry.js', import.meta.url);

Expand Down
Loading

0 comments on commit 95cd4ef

Please sign in to comment.