@@ -15,7 +15,10 @@ export const getJWPUBDatabase = async (
1515 try {
1616 const outerZip = await extractZipFiles ( buffer ) ;
1717 if ( ! outerZip . files [ "contents" ] ) {
18- throw new Error ( "No contents file found in the JWPUB file" ) ;
18+ throw createError ( {
19+ message : "No contents file found in the JWPUB file" ,
20+ status : 400 ,
21+ } ) ;
1922 }
2023
2124 const innerZip = await extractZipFiles (
@@ -25,14 +28,22 @@ export const getJWPUBDatabase = async (
2528 const dbFile = Object . keys ( innerZip . files ) . find ( ( file ) =>
2629 file . endsWith ( ".db" ) ,
2730 ) ;
28- if ( ! dbFile ) throw new Error ( "No database file found in the JWPUB file" ) ;
31+ if ( ! dbFile )
32+ throw createError ( {
33+ message : "No database file found in the JWPUB file" ,
34+ status : 400 ,
35+ } ) ;
2936
3037 const sqlDb = await innerZip . files [ dbFile ] ! . async ( "uint8array" ) ;
3138
3239 return loadDatabase ( sqlDb ) ;
3340 } catch ( e ) {
3441 console . error ( e ) ;
35- throw new Error ( "Failed to get database from .jwpub file" , { cause : e } ) ;
42+ throw createError ( {
43+ cause : e ,
44+ message : "Failed to get database from .jwpub file" ,
45+ status : 500 ,
46+ } ) ;
3647 }
3748} ;
3849
@@ -52,19 +63,28 @@ export const parseJWPUB = async (db: Database) => {
5263 }
5364
5465 if ( ! header ) {
55- throw new Error ( "No header found for: " + htmlDoc . toString ( ) ) ;
66+ throw createError ( {
67+ message : "No header found for: " + htmlDoc . toString ( ) ,
68+ status : 400 ,
69+ } ) ;
5670 }
5771
5872 if ( ! header . includes ( "Nr." ) ) {
59- throw new Error ( "Invalid header: " + header ) ;
73+ throw createError ( {
74+ message : "Invalid header: " + header ,
75+ status : 400 ,
76+ } ) ;
6077 }
6178
6279 const [ , numberStr , ...titleParts ] = header . split ( / \s + / ) ;
6380 const title = titleParts . join ( " " ) ;
6481 const number = parseInt ( numberStr || "0" ) ;
6582
6683 if ( ! numberStr || isNaN ( number ) ) {
67- throw new Error ( `Invalid number (${ numberStr } ) for header: ${ header } ` ) ;
84+ throw createError ( {
85+ message : `Invalid number (${ numberStr } ) for header: ${ header } ` ,
86+ status : 400 ,
87+ } ) ;
6888 }
6989
7090 const paragraphs = htmlDoc . querySelectorAll ( "div > p" ) ;
@@ -100,11 +120,17 @@ export const parseJWPUB = async (db: Database) => {
100120 }
101121
102122 if ( ! updatedStr ) {
103- throw new Error ( "No updated string found for: " + header ) ;
123+ throw createError ( {
124+ message : "No updated string found for: " + header ,
125+ status : 400 ,
126+ } ) ;
104127 }
105128
106129 if ( ! updatedStr . includes ( "Nr." ) ) {
107- throw new Error ( "Invalid updated string: " + updatedStr ) ;
130+ throw createError ( {
131+ message : "Invalid updated string: " + updatedStr ,
132+ status : 400 ,
133+ } ) ;
108134 }
109135
110136 updatedStr = updatedStr
@@ -116,11 +142,17 @@ export const parseJWPUB = async (db: Database) => {
116142 const updated = updatedStr . includes ( "herzien" ) ? revised : original ;
117143
118144 if ( ! updated ) {
119- throw new Error ( "No updated date found for: " + updatedStr ) ;
145+ throw createError ( {
146+ message : "No updated date found for: " + updatedStr ,
147+ status : 400 ,
148+ } ) ;
120149 }
121150
122151 if ( ! / ^ \d { 1 , 2 } \/ \d { 2 } $ / . test ( updated ) ) {
123- throw new Error ( `Invalid updated date (${ updated } ): ` + updatedStr ) ;
152+ throw createError ( {
153+ message : `Invalid updated date (${ updated } ): ` + updatedStr ,
154+ status : 400 ,
155+ } ) ;
124156 }
125157
126158 return { number, title, updated } ;
@@ -156,7 +188,10 @@ const generateSHA256Rounds = async (text: string) => {
156188
157189const xorBuffers = ( buf1 : Uint8Array , buf2 : Uint8Array ) => {
158190 if ( buf1 . length !== buf2 . length ) {
159- throw new Error ( "Buffers must be same length" ) ;
191+ throw createError ( {
192+ message : "Buffers must be same length" ,
193+ status : 400 ,
194+ } ) ;
160195 }
161196
162197 return buf1 . map ( ( byte , i ) => byte ^ buf2 [ i % buf2 . length ] ! ) ;
@@ -212,7 +247,10 @@ const getPubCard = (db: Database) => {
212247 ) ;
213248
214249 if ( publicationTable . length === 0 ) {
215- throw new Error ( "The file selected is not a valid JWPUB file." ) ;
250+ throw createError ( {
251+ message : "The file selected is not a valid JWPUB file." ,
252+ status : 400 ,
253+ } ) ;
216254 }
217255
218256 return publicationTable [ 0 ] ?. values [ 0 ] ?. join ( "_" ) ?? "" ;
0 commit comments