mirror of
https://github.com/mikpe/pdp10-tools.git
synced 2026-01-23 19:08:00 +00:00
error: new module to format error terms for diagnostics
This commit is contained in:
parent
887fdf0d93
commit
369668d362
47
erlang/apps/lib/src/error.erl
Normal file
47
erlang/apps/lib/src/error.erl
Normal file
@ -0,0 +1,47 @@
|
||||
%%% -*- erlang-indent-level: 2 -*-
|
||||
%%%
|
||||
%%% error message formatting
|
||||
%%% Copyright (C) 2019 Mikael Pettersson
|
||||
%%%
|
||||
%%% This file is part of pdp10-tools.
|
||||
%%%
|
||||
%%% pdp10-tools is free software: you can redistribute it and/or modify
|
||||
%%% it under the terms of the GNU General Public License as published by
|
||||
%%% the Free Software Foundation, either version 3 of the License, or
|
||||
%%% (at your option) any later version.
|
||||
%%%
|
||||
%%% pdp10-tools is distributed in the hope that it will be useful,
|
||||
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
%%% GNU General Public License for more details.
|
||||
%%%
|
||||
%%% You should have received a copy of the GNU General Public License
|
||||
%%% along with pdp10-tools. If not, see <http://www.gnu.org/licenses/>.
|
||||
%%%
|
||||
%%%-----------------------------------------------------------------------------
|
||||
%%%
|
||||
%%% Format error terms for output in end-user visible diagnostics.
|
||||
%%%
|
||||
%%% Our standard representation of error terms is as {Module, Reason} 2-tuples,
|
||||
%%% where Module:format_error(Reason) returns a textual representation of Reason.
|
||||
%%% These error terms are typically returned as {error, {Module, Reason}} values
|
||||
%%% or thrown as error exceptions.
|
||||
%%%
|
||||
%%% This code will _not_ attempt to load Module.
|
||||
|
||||
-module(error).
|
||||
-export([format/1]).
|
||||
|
||||
-spec format(term()) -> io_lib:chars().
|
||||
format({Module, Reason} = Error) when is_atom(Module) ->
|
||||
case erlang:function_exported(Module, format_error, 1) of
|
||||
true ->
|
||||
try Module:format_error(Reason)
|
||||
catch _:_ -> default_format(Error)
|
||||
end;
|
||||
false -> default_format(Error)
|
||||
end;
|
||||
format(Error) -> default_format(Error).
|
||||
|
||||
default_format(Error) ->
|
||||
io_lib:format("~p", [Error]).
|
||||
Loading…
x
Reference in New Issue
Block a user