Skip to content

Commit

Permalink
🐛 handle getImagesFromPDF exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdelfattah committed Aug 19, 2022
1 parent ea6dadf commit dab996d
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 17 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# CHANGELOG

## v1.3.8 (20/17/2022)
## v1.3.9 (19/08/2022)

### Fixes

- 🐛 handle getImagesFromPDF exceptions

## v1.3.8 (20/07/2022)

### Fixes

Expand Down
2 changes: 1 addition & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"componentFolder": "cypress/specs",
"componentFolder": "cypress/integration",
"testFiles": "**/*.spec.{js,ts,jsx,tsx}",
"viewportWidth": 1500,
"viewportHeight": 900,
Expand Down
Empty file added cypress/assets/broken-pdf.pdf
Empty file.
Binary file added cypress/assets/multi-page.pdf
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mount } from '@cypress/react'
import {
AnnotationLensPointerPositionTester,
AnnotationLensStateTester,
} from './helpers'
} from '../specs/helpers'

const containerHeight = 600
const containerWidth = 600
Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions cypress/integration/getImagesFromPDF.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getImagesFromPDF } from '../../dist'
import multiPage from '../assets/multi-page.pdf'
import brokenPDF from '../assets/broken-pdf.pdf'

describe('getImageFromPDF', () => {
it('should return PDF pages', () => {
getImagesFromPDF(multiPage).then((images) => {
expect(images.length).to.equal(5)
})
})

it('should catch error when the PDF is broken', () => {
getImagesFromPDF(brokenPDF).catch((error) => {
expect(error.name).to.equal('InvalidPDFException')
})
})

it('should catch error when the PDF has too many Pages', () => {
getImagesFromPDF(multiPage, 3).catch((error) => {
expect(error.name).to.equal('TooManyPagesError')
})
})
})
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-mindee-js",
"version": "1.3.8",
"version": "1.3.9",
"description": "Front-End Computer Vision SDK for React",
"author": "@mindee",
"license": "MIT",
Expand Down
23 changes: 12 additions & 11 deletions src/utils/getImagesFromPDF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ export default function getImagesFromPDF(
onSuccess?: () => void,
resolution?: number
) {
return new Promise(
(
resolve: (images: string[]) => void,
reject: (data: { error?: any; hasPageCountError?: boolean }) => void
) => {
getDocument(file).promise.then((document: PDFDocumentProxy) => {
return new Promise<string[]>((resolve, reject) => {
getDocument(file)
.promise.then((document: PDFDocumentProxy) => {
if (document.numPages > maxPages) {
reject({ hasPageCountError: true })
const error = new Error('Too many pages')
error.name = 'TooManyPagesError'
reject(error)
return
}
onSuccess?.()
Expand All @@ -72,10 +71,12 @@ export default function getImagesFromPDF(
}, [])
resolve(images)
})
.catch((e) => {
reject({ error: e })
.catch((error) => {
reject(error)
})
})
}
)
.catch((error) => {
reject(error)
})
})
}
36 changes: 34 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,7 @@ [email protected]:
rimraf "^2.6.2"
ssim.js "^3.1.1"

js-tokens@^4.0.0:
"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==
Expand Down Expand Up @@ -2108,6 +2108,13 @@ log-update@^4.0.0:
slice-ansi "^4.0.0"
wrap-ansi "^6.2.0"

loose-envify@^1.1.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
dependencies:
js-tokens "^3.0.0 || ^4.0.0"

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
Expand Down Expand Up @@ -2279,7 +2286,7 @@ npm-run-path@^4.0.0:
dependencies:
path-key "^3.0.0"

object-assign@^4.1.0:
object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
Expand Down Expand Up @@ -2543,6 +2550,15 @@ ramda@~0.27.1:
resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.27.1.tgz#66fc2df3ef873874ffc2da6aa8984658abacf5c9"
integrity sha512-PgIdVpn5y5Yns8vqb8FzBUEYn98V3xcPgawAkkgj0YJ0qDsnHCiNmZYfOGMgOvoB0eWFLpYbhxUR3mxfDIMvpw==

react-dom@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-17.0.2.tgz#ecffb6845e3ad8dbfcdc498f0d0a939736502c23"
integrity sha512-s4h96KtLDUQlsENhMn1ar8t2bEa+q/YAtj8pPPdIjPDGBDIVNsrD9aXNWqspUe6AzKCIG0C1HZZLqLV7qpOBGA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
scheduler "^0.20.2"

react-is@^17.0.1:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-17.0.2.tgz#e691d4a8e9c789365655539ab372762b0efb54f0"
Expand All @@ -2553,6 +2569,14 @@ react-refresh@^0.10.0:
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.10.0.tgz#2f536c9660c0b9b1d500684d9e52a65e7404f7e3"
integrity sha512-PgidR3wST3dDYKr6b4pJoqQFpPGNKDSCDx4cZoshjXipw3LzO7mG1My2pwEzz2JVkF+inx3xRpDeQLFQGH/hsQ==

react@^17.0.2:
version "17.0.2"
resolved "https://registry.yarnpkg.com/react/-/react-17.0.2.tgz#d0b5cc516d29eb3eee383f75b62864cfb6800037"
integrity sha512-gnhPt75i/dq/z3/6q/0asP78D0u592D5L1pd7M8P+dck6Fu/jJeL6iVVK23fptSUZj8Vjf++7wXA8UNclGQcbA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

rechoir@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.6.2.tgz#85204b54dba82d5742e28c96756ef43af50e3384"
Expand Down Expand Up @@ -2688,6 +2712,14 @@ safer-buffer@^2.0.2, safer-buffer@^2.1.0, safer-buffer@~2.1.0:
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

scheduler@^0.20.2:
version "0.20.2"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.20.2.tgz#4baee39436e34aa93b4874bddcbf0fe8b8b50e91"
integrity sha512-2eWfGgAqqWFGqtdMmcL5zCMK1U8KlXv8SQFGglL3CEtd0aDVDWgeF/YoCmvln55m5zSk3J/20hTaSBeSObsQDQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

semver@^5.5.0:
version "5.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
Expand Down

0 comments on commit dab996d

Please sign in to comment.