|
| 1 | +defmodule ErrorTracker.Stacktrace do |
| 2 | + use Ecto.Schema |
| 3 | + |
| 4 | + @primary_key false |
| 5 | + embedded_schema do |
| 6 | + embeds_many :lines, Line, primary_key: false do |
| 7 | + field :application, :string |
| 8 | + field :module, :string |
| 9 | + field :function, :string |
| 10 | + field :arity, :integer |
| 11 | + field :file, :string |
| 12 | + field :line, :integer |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + def new(stack) do |
| 17 | + lines_params = |
| 18 | + for {module, function, arity, opts} <- stack do |
| 19 | + application = Application.get_application(module) |
| 20 | + |
| 21 | + %{ |
| 22 | + application: to_string(application), |
| 23 | + module: to_string(module), |
| 24 | + function: to_string(function), |
| 25 | + arity: arity, |
| 26 | + file: to_string(opts[:file]), |
| 27 | + line: opts[:line] |
| 28 | + } |
| 29 | + end |
| 30 | + |
| 31 | + %__MODULE__{} |
| 32 | + |> Ecto.Changeset.cast(%{lines: lines_params}, []) |
| 33 | + |> Ecto.Changeset.cast_embed(:lines, with: &line_changeset/2) |
| 34 | + |> Ecto.Changeset.apply_action(:new) |
| 35 | + end |
| 36 | + |
| 37 | + defp line_changeset(line = %__MODULE__.Line{}, params) do |
| 38 | + Ecto.Changeset.cast(line, params, ~w[application module function arity file line]a) |
| 39 | + end |
| 40 | + |
| 41 | + def source(stack = %__MODULE__{}) do |
| 42 | + List.first(stack.lines) |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +defimpl String.Chars, for: ErrorTracker.Stacktrace do |
| 47 | + def to_string(stack = %ErrorTracker.Stacktrace{}) do |
| 48 | + Enum.join(stack.lines, "\n") |
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | +defimpl String.Chars, for: ErrorTracker.Stacktrace.Line do |
| 53 | + def to_string(stack_line = %ErrorTracker.Stacktrace.Line{}) do |
| 54 | + "#{stack_line.module}.#{stack_line.function}/#{stack_line.arity} in #{stack_line.file}:#{stack_line.line}" |
| 55 | + end |
| 56 | +end |
0 commit comments