@@ -4,7 +4,11 @@ type runtime
44type tabsApi
55type tab = Js .Json .t
66type urlLookup = result <string , string >
7- type fillResult = result <unit , string >
7+ type fillSuccess = {
8+ bytes : int ,
9+ filename : string ,
10+ }
11+ type fillResult = result <fillSuccess , string >
812
913@val @scope ("browser" )
1014external runtime : runtime = "runtime"
@@ -30,6 +34,16 @@ let triggerDownload: (string, string) => unit = %raw(`(url, filename) => {
3034}` )
3135
3236let makeFilledFilename : unit => string = %raw (` () => " blocky-writer-filled-" + Date .now () + " .pdf" ` )
37+ let errorDetails : 'a => string = %raw (` (error ) => {
38+ if (error && typeof error === " object" && " message" in error && error .message ) {
39+ return String (error .message );
40+ }
41+ try {
42+ return String (error);
43+ } catch {
44+ return " unknown error" ;
45+ }
46+ }` )
3347
3448let unsafeJson = (value : 'a ): Js .Json .t => Obj .magic (value )
3549
@@ -80,7 +94,7 @@ let getActivePdfUrl = (): Js.Promise.t<urlLookup> => {
8094
8195 Js .Promise2 .catch (lookupPromise , err => {
8296 Js .log2 ("active tab lookup failed" , err )
83- Js .Promise .resolve (Error ("failed to query active tab" ))
97+ Js .Promise .resolve (Error ("failed to query active tab: " ++ errorDetails ( err ) ))
8498 })
8599}
86100
@@ -123,7 +137,7 @@ let detectBlocksForActiveTab = (): Js.Promise.t<detectResult> => {
123137
124138 Js .Promise2 .catch (detectPromise , err => {
125139 Js .log2 ("popup detect failed" , err )
126- Js .Promise .resolve ({ok : false , blocks : [], error : Some ("request failed" )})
140+ Js .Promise .resolve ({ok : false , blocks : [], error : Some ("request failed: " ++ errorDetails ( err ) )})
127141 })
128142}
129143
@@ -140,7 +154,8 @@ let fillPdfAndDownload = (
140154 Js .Promise2 .then (response -> Webapi .Fetch .Response .arrayBuffer , pdfBuffer =>
141155 Js .Promise2 .then (PdfTool .fillBlocks (pdfBuffer , blocks , fields ), filledBuffer => {
142156 let nativeBuffer : Js .Typed_array .ArrayBuffer .t = Obj .magic (filledBuffer )
143- if Js .Typed_array .ArrayBuffer .byteLength (nativeBuffer ) == 0 {
157+ let byteLength = Js .Typed_array .ArrayBuffer .byteLength (nativeBuffer )
158+ if byteLength == 0 {
144159 Js .Promise .resolve (Error ("fill_blocks returned an empty PDF payload" ))
145160 } else {
146161 let blobPart = Webapi .Blob .arrayBufferToBlobPart (nativeBuffer )
@@ -149,9 +164,10 @@ let fillPdfAndDownload = (
149164 Webapi .Blob .makeBlobPropertyBag (~_type = "application/pdf" , ()),
150165 )
151166 let objectUrl = Webapi .Url .createObjectURLFromBlob (blob )
152- triggerDownload (objectUrl , makeFilledFilename ())
167+ let filename = makeFilledFilename ()
168+ triggerDownload (objectUrl , filename )
153169 let _timeoutId = Js .Global .setTimeout (() => Webapi .Url .revokeObjectURL (objectUrl ), 60000 )
154- Js .Promise .resolve (Ok (() ))
170+ Js .Promise .resolve (Ok ({ bytes : byteLength , filename } ))
155171 }
156172 })
157173 )
@@ -161,7 +177,7 @@ let fillPdfAndDownload = (
161177
162178 Js .Promise2 .catch (fillPromise , err => {
163179 Js .log2 ("fill and download failed" , err )
164- Js .Promise .resolve (Error ("fill and download request failed" ))
180+ Js .Promise .resolve (Error ("fill and download request failed: " ++ errorDetails ( err ) ))
165181 })
166182}
167183
@@ -210,14 +226,21 @@ module App = {
210226 Js .Promise2 .then (fillPdfAndDownload (~blocks , ~fields ), result => {
211227 setIsLoading (_ => false )
212228 switch result {
213- | Ok (()) => setStatus (_ => "Filled PDF downloaded" )
229+ | Ok (success ) =>
230+ setStatus (_ =>
231+ "Filled PDF downloaded: "
232+ ++ Belt .Int .toString (success .bytes )
233+ ++ " bytes ("
234+ ++ success .filename
235+ ++ ")"
236+ )
214237 | Error (message ) => setStatus (_ => "Fill failed: " ++ message )
215238 }
216239 Js .Promise .resolve (())
217240 })
218- let _ = Js .Promise2 .catch (fillPromise , _ => {
241+ let _ = Js .Promise2 .catch (fillPromise , err => {
219242 setIsLoading (_ => false )
220- setStatus (_ => "Fill failed: unexpected error" )
243+ setStatus (_ => "Fill failed: unexpected error: " ++ errorDetails ( err ) )
221244 Js .Promise .resolve (())
222245 })
223246 ()
0 commit comments