Skip to content

Commit 517d307

Browse files
committed
Add phantomjs error handlers
1 parent a7bcbe5 commit 517d307

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

lib/scripts/pdf_a4_portrait.js

Lines changed: 22 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/scripts/pdf_a4_portrait.coffee

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ exit = (error) ->
88
phantom.exit(if error then 1 else 0)
99

1010

11+
# Build stack to print
12+
buildStack = (msg, trace) ->
13+
msgStack = [msg]
14+
if trace?.length
15+
msgStack.push('TRACE:')
16+
trace.forEach (t) ->
17+
msgStack.push(" at #{t.file || t.sourceURL}: #{t.line} (in function #{t.function})")
18+
msgStack.join('\n')
19+
20+
21+
phantom.onError = (msg, trace) ->
22+
exit(buildStack('Script - '+ msg, trace))
23+
24+
1125
# Force cleanup after 2 minutes
1226
setTimeout ->
1327
exit('Force timeout')
@@ -25,9 +39,13 @@ page.viewportSize = vp if vp = options.viewportSize
2539
totalPages = 0
2640

2741

42+
page.onError = (msg, trace) ->
43+
exit(buildStack('Evaluation - '+ msg, trace))
44+
45+
2846
# Set up content
2947
# --------------
30-
content = page.evaluate ->
48+
content = page.evaluate (options) ->
3149
styles = document.querySelector('head style')?.outerHTML || ''
3250
if $header = document.getElementById('pageHeader')
3351
header = $header.outerHTML
@@ -79,7 +97,6 @@ paper.footer?.height ?= '28mm'
7997
page.paperSize = paper
8098

8199

82-
83100
# Completely load page & end process
84101
# ----------------------------------
85102
page.onLoadFinished = (status) ->

test/index.coffee

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ html = """
2020
</html>
2121
"""
2222

23-
23+
#
24+
# API
25+
#
2426
test 'pdf.create(html[, options]) throws an error when executing without html', (st) ->
2527
st.plan(3)
2628

@@ -53,9 +55,10 @@ test 'pdf.create(html[, options], callback) returns error as first cb argument w
5355
test 'pdf.create(html[, options]).toFile([filename, ]callback)', (st) ->
5456
st.plan(5)
5557

56-
pdf.create(html).toFile (err, pdf) =>
58+
pdf.create(html).toFile (err, pdf) ->
5759
st.error(err)
5860
st.assert(typeof pdf.filename == 'string', "toFile(callback) returns {filename: '#{pdf.filename}'} as second cb argument")
61+
fs.unlink(pdf.filename)
5962

6063
file = path.join(__dirname,'simple.pdf')
6164
pdf.create(html).toFile file, (err, pdf) ->
@@ -74,13 +77,21 @@ test 'pdf.create(html[, options]).toBuffer(callback)', (st) ->
7477

7578

7679
test 'pdf.create(html[, options]).toStream(callback)', (st) ->
77-
st.plan(2)
80+
st.plan(3)
7881

7982
stream = pdf.create(html).toStream (err, stream) ->
8083
st.error(err)
81-
st.assert(stream instanceof fs.ReadStream, "toStream(callback) returns a fs.ReadableStream as second cb argument")
84+
st.assert(stream instanceof fs.ReadStream, "toStream(callback) returns a fs.ReadStream as second cb argument")
85+
destination = __dirname+'/streamed.pdf'
86+
stream.pipe fs.createWriteStream(destination)
87+
stream.on 'end', ->
88+
st.assert(fs.existsSync(destination), 'toStream returns a working readable stream')
89+
fs.unlink(destination)
8290

8391

92+
#
93+
# Options
94+
#
8495
test 'allows custom html and css', (st) ->
8596
st.plan(3)
8697

0 commit comments

Comments
 (0)