Mange / roadie
1
# frozen_string_literal: true
2

3 5
module Roadie
4
  # Base class for all Roadie errors. Rescue this if you want to catch errors
5
  # from Roadie.
6
  #
7
  # If Roadie raises an error that does not inherit this class, please report
8
  # it as a bug.
9 5
  class Error < RuntimeError; end
10

11
  # Raised when Roadie encounters an invalid URL which cannot be parsed by
12
  # Ruby's +URI+ class.
13
  #
14
  # This could be a hint that something in your HTML or CSS is broken.
15 5
  class InvalidUrlPath < Error
16
    # The original error, raised from +URI+.
17 5
    attr_reader :cause
18

19 5
    def initialize(given_path, cause = nil)
20 5
      @cause = cause
21 5
      if cause
22 5
        cause_message = " Caused by: #{cause}"
23
      else
24 0
        cause_message = ""
25
      end
26 5
      super "Cannot use path \"#{given_path}\" in URL generation.#{cause_message}"
27
    end
28
  end
29

30
  # Raised when an asset provider cannot find a stylesheet.
31
  #
32
  # If you are writing your own asset provider, make sure to raise this in the
33
  # +#find_stylesheet!+ method.
34
  #
35
  # @see AssetProvider
36 5
  class CssNotFound < Error
37
    # The name of the stylesheet that cannot be found
38 5
    attr_reader :css_name
39

40
    # Provider used when finding
41 5
    attr_reader :provider
42

43
    # Extra message
44 5
    attr_reader :extra_message
45

46
    # TODO: Change signature in the next major version of Roadie.
47 5
    def initialize(css_name, extra_message = nil, provider = nil)
48 5
      @css_name = css_name
49 5
      @provider = provider
50 5
      @extra_message = extra_message
51 5
      super build_message(extra_message)
52
    end
53

54 5
    protected
55 5
    def error_row
56 5
      "#{provider || "Unknown provider"}: #{extra_message || message}"
57
    end
58

59 5
    private
60
    # Redundant method argument is to keep API compatability without major version bump.
61
    # TODO: Remove argument on version 4.0.
62 5
    def build_message(extra_message = @extra_message)
63 5
      message = +%(Could not find stylesheet "#{css_name}")
64 5
      message << ": #{extra_message}" if extra_message
65 5
      message << "\nUsed provider:\n#{provider}" if provider
66 5
      message
67
    end
68
  end
69

70 5
  class ProvidersFailed < CssNotFound
71 5
    attr_reader :errors
72

73 5
    def initialize(css_name, provider_list, errors)
74 5
      @errors = errors
75 5
      super(css_name, "All providers failed", provider_list)
76
    end
77

78 5
    private
79 5
    def build_message(extra_message)
80 5
      message = +%(Could not find stylesheet "#{css_name}": #{extra_message}\nUsed providers:\n)
81 5
      each_error_row(errors) do |row|
82 5
        message << "\t" << row << "\n"
83
      end
84 5
      message
85
    end
86

87 5
    def each_error_row(errors)
88 5
      errors.each do |error|
89 5
        case error
90
        when ProvidersFailed
91 5
          each_error_row(error.errors) { |row| yield row }
92
        when CssNotFound
93 5
          yield error.error_row
94
        else
95 0
          yield "Unknown provider (#{error.class}): #{error}"
96
        end
97
      end
98
    end
99
  end
100
end

Read our documentation on viewing source code .

Loading