- 
                Notifications
    
You must be signed in to change notification settings  - Fork 3.5k
 
pretty format terms in errors #14269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
634a766
              531ac72
              f395f92
              9a5c2b6
              9c65685
              fd7dd53
              aaf1ae9
              c05bd84
              269d7f2
              a334a3f
              51d82cf
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -177,6 +177,16 @@ defmodule Exception do | |
| end | ||
| end | ||
| 
     | 
||
| @spec format(String.t(), any) :: String.t() | ||
| def format_message_with_term(message, term) do | ||
| inspected = | ||
| term | ||
| |> inspect(pretty: true) | ||
| |> String.replace(~r/^(?=.+)/m, " ") | ||
                
       | 
||
| 
     | 
||
| message <> "\n\n" <> inspected | ||
| end | ||
| 
     | 
||
| @doc """ | ||
| Attaches information to exceptions for extra debugging. | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -1431,7 +1441,10 @@ defmodule BadStructError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "expected a struct named #{inspect(exception.struct)}, got: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "expected a struct named #{inspect(exception.struct)}, got:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -1451,7 +1464,10 @@ defmodule BadMapError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "expected a map, got: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "expected a map, got:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -1470,7 +1486,10 @@ defmodule BadBooleanError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "expected a boolean on left-side of \"#{exception.operator}\", got: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "expected a boolean on left-side of \"#{exception.operator}\", got:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -1492,7 +1511,10 @@ defmodule MatchError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "no match of right hand side value: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "no match of right hand side value:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
        
          
        
         | 
    @@ -1518,7 +1540,10 @@ defmodule CaseClauseError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "no case clause matching: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "no case clause matching:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -1548,7 +1573,10 @@ defmodule WithClauseError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "no with clause matching: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "no with clause matching:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -1598,7 +1626,10 @@ defmodule TryClauseError do | |
| 
     | 
||
| @impl true | ||
| def message(exception) do | ||
| "no try clause matching: #{inspect(exception.term)}" | ||
| Exception.format_message_with_term( | ||
| "no try clause matching:", | ||
| exception.term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
          
            
          
           | 
    @@ -2160,7 +2191,10 @@ defmodule KeyError do | |
| "make sure to add parentheses after the function name)" | ||
| 
     | 
||
| true -> | ||
| message <> " in: #{inspect(term, pretty: true, limit: :infinity)}" | ||
| Exception.format_message_with_term( | ||
| message <> " in:", | ||
| term | ||
| ) | ||
| end | ||
| end | ||
| 
     | 
||
| 
          
            
          
           | 
    ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should mark it as @doc false. Preferably underscore it too.