[m-rev.] IO error type

Peter Wang novalazy at gmail.com
Wed Aug 17 17:59:12 AEST 2022


On Sun, 14 Aug 2022 10:38:00 +0200 Volker Wysk <post at volker-wysk.de> wrote:
> 
> The easier (and better?) way would be to make use of the errno value
> only. The table maps an errno value (an integer) to the name of the
> error (a string, for instance "ENOENT"). It would also be possible to
> define a big enumeration type, which enumerates all the known errno
> values. 
> 
> For instance, a function like this could be added to the io module,
> which maps an IO error to the name of the errno:
> 
> :- func errno(io.error) = string.
> 
> This way, it would be possible to do something depending on *which* IO
> error occured, other than generating an error message.

Hi,

My proposal from Feb 2022 was this:

    :- type system_error.

    :- func make_io_error(system_error, string) = io.error.

    :- pred get_system_error(io.error::in, system_error::out)
        is semidet.

That is, to introduce a backend/platform specific system_error type,
and add a field for system_errors in the io.error type.
(It would still be possible to construct an io.error without a
system_error value.)

If we add your suggestion to allow converting an io.error to a name,
we might have:

	% Returns a standard error name if the given error corresponds
	% to a system error known to the Mercury system, e.g. "ENOENT"
	% (or possibly the name of a Java or C# exception?).
	% Fails if the error is not a system error, or if the error is
	% not one recognised by the Mercury system.
	%
    :- pred get_error_name(io.error::in, string::out)
	is semidet.

	% As above, but takes a system_error instead.
	%
    :- pred get_system_error_name(system_error::in, string::out)
	is semidet.

Peter

PS. Other sources of errno names to scrape:

  - https://github.com/jart/cosmopolitan/blob/master/libc/sysv/consts.sh
  - https://docs.python.org/3/library/errno.html


More information about the reviews mailing list