|
| 1 | +defmodule RetWeb.ControllerHelpersTest do |
| 2 | + use ExUnit.Case, async: true |
| 3 | + |
| 4 | + alias RetWeb.ControllerHelpers |
| 5 | + import ExUnit.CaptureLog |
| 6 | + require Logger |
| 7 | + |
| 8 | + setup_all do |
| 9 | + Logger.configure(level: :info) |
| 10 | + :ok |
| 11 | + end |
| 12 | + |
| 13 | + describe "log_our_code_location/3" do |
| 14 | + @tag :error_logging |
| 15 | + test "ignores dependency modules and finds innermost project module" do |
| 16 | + log = |
| 17 | + capture_log([level: :info], fn -> |
| 18 | + stacktrace = [ |
| 19 | + {Bamboo.Email, :new_email, 0, |
| 20 | + [file: ~c"_build/test/lib/bamboo/ebin/Elixir.Bamboo.Email.beam", line: 190]}, |
| 21 | + {RetWeb.Email, :auth_email, 2, [file: ~c"lib/ret_web/email.ex", line: 19]}, |
| 22 | + {RetWeb.Endpoint, :get_cors_origins, 0, |
| 23 | + [file: ~c"lib/ret_web/endpoint.ex", line: 10]}, |
| 24 | + {RetWeb.Endpoint, :allowed_origin?, 1, [file: ~c"lib/ret_web/endpoint.ex", line: 16]} |
| 25 | + ] |
| 26 | + |
| 27 | + ControllerHelpers.log_our_code_location(stacktrace, :email_error, "Pseudo-failure") |
| 28 | + end) |
| 29 | + |
| 30 | + assert log =~ "Pseudo-failure" |
| 31 | + assert log =~ "at email.ex:19" |
| 32 | + assert log =~ "calling Bamboo.Email.new_email" |
| 33 | + assert log =~ ":email_error" |
| 34 | + assert log =~ "For full stacktraces, set the environment variable STACKTRACE to FULL." |
| 35 | + refute log =~ "Stack trace (most recent call first)" |
| 36 | + end |
| 37 | + |
| 38 | + @tag :error_logging |
| 39 | + test "falls back to the first entry if no project module is found" do |
| 40 | + log = |
| 41 | + capture_log([level: :info], fn -> |
| 42 | + stacktrace = [ |
| 43 | + {Plug.Conn, :send_resp, 3, [file: ~c"deps/plug/lib/plug/spam.ex", line: 400]}, |
| 44 | + {Phoenix.Controller, :render, 3, |
| 45 | + [file: ~c"deps/phoenix/lib/phoenix/controller.ex", line: 100]} |
| 46 | + ] |
| 47 | + |
| 48 | + ControllerHelpers.log_our_code_location(stacktrace, :another_error) |
| 49 | + end) |
| 50 | + |
| 51 | + assert log =~ "Failure" |
| 52 | + assert log =~ "at spam.ex:400" |
| 53 | + assert log =~ "calling unknown" |
| 54 | + assert log =~ ":another_error" |
| 55 | + assert log =~ "For full stacktraces, set the environment variable STACKTRACE to FULL." |
| 56 | + refute log =~ "Stack trace (most recent call first)" |
| 57 | + end |
| 58 | + |
| 59 | + @tag :error_logging |
| 60 | + test "handles malformed stacktrace entries by logging full stack trace" do |
| 61 | + log = |
| 62 | + capture_log([level: :info], fn -> |
| 63 | + # This should trigger the rescue block because pattern doesn't match |
| 64 | + stacktrace = [ |
| 65 | + {:not, :a, :standard, :entry} |
| 66 | + ] |
| 67 | + |
| 68 | + ControllerHelpers.log_our_code_location(stacktrace, :spam_error, "Probe failure") |
| 69 | + end) |
| 70 | + |
| 71 | + assert log =~ "Probe failure" |
| 72 | + assert log =~ "at <malformed stacktrace>:0" |
| 73 | + assert log =~ "calling unknown" |
| 74 | + assert log =~ ":spam_error" |
| 75 | + refute log =~ "For full stacktraces, set the environment variable STACKTRACE to FULL." |
| 76 | + assert log =~ "Stack trace (nonstandard)" |
| 77 | + end |
| 78 | + |
| 79 | + @tag :error_logging |
| 80 | + test "handles empty stacktrace by logging full stacktrace" do |
| 81 | + log = |
| 82 | + capture_log([level: :info], fn -> |
| 83 | + # This should trigger the rescue block because there's no entry to match |
| 84 | + ControllerHelpers.log_our_code_location([], :strange_error, "Weird failure") |
| 85 | + end) |
| 86 | + |
| 87 | + assert log =~ "Weird failure" |
| 88 | + assert log =~ "at <malformed stacktrace>:0" |
| 89 | + assert log =~ "calling unknown" |
| 90 | + assert log =~ ":strange_error" |
| 91 | + refute log =~ "For full stacktraces, set the environment variable STACKTRACE to FULL." |
| 92 | + assert log =~ "Stack trace (most recent call first)" |
| 93 | + end |
| 94 | + |
| 95 | + @tag :error_logging |
| 96 | + test "logs full stacktrace when STACKTRACE environment variable is set to FULL" do |
| 97 | + # Note: manipulation of this environment variable within async tests is deemed safe because it only relates to this module. |
| 98 | + System.put_env("STACKTRACE", "FULL") |
| 99 | + |
| 100 | + on_exit(fn -> |
| 101 | + System.delete_env("STACKTRACE") |
| 102 | + end) |
| 103 | + |
| 104 | + log = |
| 105 | + capture_log([level: :info], fn -> |
| 106 | + stacktrace = [ |
| 107 | + {RetWeb.Email, :auth_email, 2, [file: ~c"lib/ret_web/email.ex", line: 19]}, |
| 108 | + {RetWeb.Endpoint, :get_cors_origins, 0, [file: ~c"lib/ret_web/endpoint.ex", line: 10]} |
| 109 | + ] |
| 110 | + |
| 111 | + ControllerHelpers.log_our_code_location(stacktrace, :full_error, "Full failure") |
| 112 | + end) |
| 113 | + |
| 114 | + assert log =~ "Full failure" |
| 115 | + assert log =~ "at email.ex:19" |
| 116 | + assert log =~ "calling unknown" |
| 117 | + assert log =~ ":full_error" |
| 118 | + refute log =~ "For full stacktraces, set the environment variable STACKTRACE to FULL." |
| 119 | + assert log =~ "Stack trace (most recent call first)" |
| 120 | + assert log =~ "RetWeb.Email.auth_email/2" |
| 121 | + assert log =~ "RetWeb.Endpoint.get_cors_origins/0" |
| 122 | + end |
| 123 | + |
| 124 | + @tag :error_logging |
| 125 | + test "handles stacktrace entry without filepath by returning <unknown>" do |
| 126 | + log = |
| 127 | + capture_log([level: :info], fn -> |
| 128 | + # Entry with arity but no location information |
| 129 | + stacktrace = [ |
| 130 | + {RetWeb.HealthController, :index, 2, []} |
| 131 | + ] |
| 132 | + |
| 133 | + ControllerHelpers.log_our_code_location( |
| 134 | + stacktrace, |
| 135 | + :no_filepath_error, |
| 136 | + "No-filepath failure" |
| 137 | + ) |
| 138 | + end) |
| 139 | + |
| 140 | + assert log =~ "No-filepath failure" |
| 141 | + assert log =~ "at <unknown>:0" |
| 142 | + assert log =~ "calling unknown" |
| 143 | + assert log =~ ":no_filepath_error" |
| 144 | + refute log =~ "For full stacktraces, set the environment variable STACKTRACE to FULL." |
| 145 | + assert log =~ "Stack trace (most recent call first)" |
| 146 | + assert log =~ "RetWeb.HealthController.index/2" |
| 147 | + end |
| 148 | + end |
| 149 | +end |
0 commit comments