Skip to content

Commit c6f4801

Browse files
committed
ref: Convert runtime error to InvalidScreenshotFormatError
1 parent cbf33fd commit c6f4801

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

lib/ferrum/errors.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ def initialize(message = "Could not compute content quads")
7878
end
7979
end
8080

81+
class InvalidScreenshotFormatError < Error
82+
def initialize(format)
83+
super("Invalid value #{format} for option `:format` (#{Page::Screenshot::SUPPORTED_SCREENSHOT_FORMAT.join(' | ')})")
84+
end
85+
end
86+
8187
class BrowserError < Error
8288
attr_reader :response
8389

lib/ferrum/page/screenshot.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ module Screenshot
7575
# page.screenshot(path: "google.jpg") # => 30902
7676
#
7777
# @example Save to Base64 in WebP with reduce quality:
78-
# page.screenshot(format: 'webp', quality: 60) # "iVBORw0KGgoAAAANS...
78+
# page.screenshot(format: "webp", quality: 60) # "iVBORw0KGgoAAAANS...
7979
#
8080
# @example Save to Base64 the whole page not only viewport and reduce quality:
81-
# page.screenshot(full: true, format: 'jpeg', quality: 60) # "iVBORw0KGgoAAAANS...
81+
# page.screenshot(full: true, format: "jpeg", quality: 60) # "iVBORw0KGgoAAAANS...
8282
#
8383
# @example Save with specific background color:
8484
# page.screenshot(background_color: Ferrum::RGBA.new(0, 0, 0, 0.0))
@@ -216,24 +216,24 @@ def screenshot_options(path = nil, format: nil, scale: 1.0, **options)
216216
screenshot_options
217217
end
218218

219-
def format_options(screenshot_format, path, quality)
220-
if !screenshot_format && path # try to infer from path
221-
extension = File.extname(path).delete(".")&.downcase
222-
screenshot_format = extension if extension && !extension.empty?
219+
def format_options(format, path, quality)
220+
if !format && path # try to infer from path
221+
extension = File.extname(path).delete(".").downcase
222+
format = extension unless extension.empty?
223223
end
224224

225-
screenshot_format ||= DEFAULT_SCREENSHOT_FORMAT
226-
screenshot_format = screenshot_format.to_s
227-
unless SUPPORTED_SCREENSHOT_FORMAT.include?(screenshot_format)
228-
raise "Not supported options `:format` #{screenshot_format}. #{SUPPORTED_SCREENSHOT_FORMAT.join(' | ')}"
225+
format ||= DEFAULT_SCREENSHOT_FORMAT
226+
format = format.to_s
227+
unless SUPPORTED_SCREENSHOT_FORMAT.include?(format)
228+
raise Ferrum::InvalidScreenshotFormatError, format
229229
end
230230

231-
screenshot_format = "jpeg" if screenshot_format == "jpg"
231+
format = "jpeg" if format == "jpg"
232232

233233
# Chrome supports screenshot qualities for JPEG and WebP
234-
quality ||= 75 if screenshot_format != "png"
234+
quality ||= 75 if format != "png"
235235

236-
[screenshot_format, quality]
236+
[format, quality]
237237
end
238238

239239
def area_options(full, selector, scale, area = nil)

0 commit comments

Comments
 (0)