• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

christhekeele / matcha / c6283b1b9bb7327327b0d71ceeefc0a6376715f6

21 Nov 2023 06:40PM UTC coverage: 63.884% (+0.1%) from 63.782%
c6283b1b9bb7327327b0d71ceeefc0a6376715f6

push

github

christhekeele
Change 'source' accessor to 'raw'; emphasize 'raw' terminology in docs.

3 of 18 new or added lines in 7 files covered. (16.67%)

2 existing lines in 2 files now uncovered.

398 of 623 relevant lines covered (63.88%)

216.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

23.08
/lib/matcha/error.ex
1
defmodule Matcha.Error do
2
  @moduledoc """
3
  Standard behaviour for `Matcha` errors.
4
  """
5

6
  alias __MODULE__
7

8
  @type message :: binary | charlist
9
  @type error_problem :: {:error, message}
10
  @type warning_problem :: {:warning, message}
11
  @type problem :: error_problem | warning_problem
12
  @type problems :: [problem]
13

14
  @doc """
15
  Generates the "prelude" text for errors in the struct this error handles
16
  into a string displayable in an error message.
17
  """
18
  @callback format_prelude(struct()) :: binary
19

20
  @doc """
21
  Converts the struct this error handles
22
  into a string displayable in an error message.
23
  """
24
  @callback format_source(struct()) :: binary
25

26
  defmacro __using__(source_type: source_type) do
×
27
    quote do
28
      @behaviour unquote(__MODULE__)
29

30
      defexception [:source, :problems, details: nil]
31

32
      @type t :: %unquote(__CALLER__.module){
×
33
              source: unquote(source_type),
34
              problems: Matcha.Error.problems()
35
            }
36

37
      @spec message(t()) :: binary
38
      @doc """
39
      Produces a human-readable message from the given `error`.
40
      """
41
      def message(%__MODULE__{} = error) do
42
        [
43
          Enum.join([format_prelude(error.source), error.details], ": "),
44
          "    ",
45
          String.replace(format_source(error.source), "\n", "\n    ")
46
          | Enum.map(error.problems, &unquote(__MODULE__).format_problem/1)
47
        ]
48
        |> Enum.join("\n ")
49
      end
50

51
      defoverridable(message: 1)
52
    end
53
  end
54

55
  def format_problem({type, problem}), do: "  #{type}: #{problem}"
54✔
56
end
57

58
defmodule Matcha.Rewrite.Error do
59
  @moduledoc """
60
  Error raised when rewriting Elixir code into a match pattern/spec.
61
  """
62

63
  alias Matcha.Error
64
  alias Matcha.Rewrite
65

66
  use Error, source_type: Rewrite.t()
67

68
  @impl Error
69
  @spec format_prelude(Rewrite.t()) :: binary
70
  def format_prelude(%Rewrite{} = _rewrite) do
54✔
71
    "found problems rewriting code into a match spec"
72
  end
73

74
  @impl Error
75
  @spec format_source(Rewrite.t()) :: binary
76
  def format_source(%Rewrite{} = rewrite) do
77
    Macro.to_string(Rewrite.code(rewrite))
54✔
78
  end
79
end
80

81
defmodule Matcha.Pattern.Error do
82
  @moduledoc """
83
  Error raised when a `Matcha.Pattern` is invalid.
84
  """
85

86
  alias Matcha.Error
87
  alias Matcha.Pattern
88

89
  use Error, source_type: Pattern.t()
90

91
  @impl Error
92
  @spec format_prelude(Pattern.t()) :: binary
93
  def format_prelude(%Pattern{} = _pattern) do
×
94
    "found problems with match pattern"
95
  end
96

97
  @impl Error
98
  @spec format_source(Pattern.t()) :: binary
99
  def format_source(%Pattern{} = pattern) do
NEW
100
    inspect(Pattern.raw(pattern))
×
101
  end
102
end
103

104
defmodule Matcha.Filter.Error do
105
  @moduledoc """
106
  Error raised when a `Matcha.Filter` is invalid.
107
  """
108

109
  alias Matcha.Error
110
  alias Matcha.Filter
111

112
  use Error, source_type: Filter.t()
113

114
  @impl Error
115
  @spec format_prelude(Filter.t()) :: binary
116
  def format_prelude(%Filter{} = _pattern) do
×
117
    "found problems with match filter"
118
  end
119

120
  @impl Error
121
  @spec format_source(Filter.t()) :: binary
122
  def format_source(%Filter{} = pattern) do
NEW
123
    inspect(Filter.raw(pattern))
×
124
  end
125
end
126

127
defmodule Matcha.Spec.Error do
128
  @moduledoc """
129
  Error raised when a `Matcha.Spec` is invalid.
130
  """
131

132
  alias Matcha.Error
133
  alias Matcha.Spec
134

135
  use Error, source_type: Spec.t()
136

137
  @impl Error
138
  @spec format_prelude(Spec.t()) :: binary
139
  def format_prelude(%Spec{} = _spec) do
×
140
    "found problems with match spec"
141
  end
142

143
  @impl Error
144
  @spec format_source(Spec.t()) :: binary
145
  def format_source(%Spec{} = spec) do
NEW
146
    inspect(Spec.raw(spec))
×
147
  end
148
end
149

150
defmodule Matcha.Trace.Error do
151
  @moduledoc """
152
  Error raised when trying to trace events happening in a running system.
153
  """
154

155
  alias Matcha.Error
156
  alias Matcha.Trace
157

158
  use Error, source_type: Trace.t()
159

160
  @impl Error
161
  @spec format_prelude(Trace.t()) :: binary
162
  def format_prelude(%Trace{} = _trace) do
×
163
    "found problems tracing"
164
  end
165

166
  @impl Error
167
  @spec format_source(Trace.t()) :: binary
168
  def format_source(%Trace{} = trace) do
169
    inspect(trace)
×
170
  end
171
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc