SRFI 36: I/O Conditions

Title

I/O Conditions

Author

Michael Sperber

Status

This SRFI is currently in ``final'' status. To see an explanation of each status that a SRFI can hold, see here. It will remain in draft until 2002-10-20, or as amended. to provide input on this SRFI, please mail to srfi minus 36 at srfi dot schemers dot org. See instructions here to subscribe to the list. You can access the discussion via the archive of the mailing list. You can access post-finalization messages via the archive of the mailing list.

Abstract

This SRFI specifies a set of condition types for I/O errors. The condition types are defined in terms of SRFI 35. Moreover, this SRFI requires a Scheme system implementing it to raise exceptions in the sense of SRFI 34 for errors occurring during the execution of the R5RS I/O operations.

Rationale

Currently, the I/O operations specified by R5RS are of only limited use to the practical programmers, as they may fail in a variety of unpredictable ways. R5RS requires the operations to signal an error if they fail. This means that the program cannot react to failures at all, even though there is no way to prevent them beforehand, and meaningful recovery may be possible.

Therefore, this SRFI requires a Scheme system to communicate I/O failures to the program via the SRFI 34 mechanism. Moreover, it requires the Scheme system to communicate the nature of the problem through a condition belonging to one of the condition types specified here. The hierarchy is fairly fine-grained to allow for the precise communication of the nature of a problem.

Specification

I/O Condition Types

This section specifies a set of I/O-related condition types via their definitions in terms of SRFI 35 along with explanations of their intended meanings.

Hierarchy

The following list depicts the I/O condition hierarchy; more detailed explanations of the condition types follow.

Intended Meanings

(define-condition-type &i/o-error &error
  i/o-error?)

This is a supertype for a set of more specific I/O errors.

(define-condition-type &i/o-port-error &i/o-error
  i/o-port-error?
  (port i/o-error-port))

This condition type specifies an I/O error that occurred during an operation on a port. Condition objects belonging to this type must specify a port in the port field.

(define-condition-type &i/o-read-error &i/o-port-error
  i/o-read-error?)

This condition type specifies a read error that occurred during an operation on a port.

(define-condition-type &i/o-write-error &i/o-port-error
  i/o-write-error?)

This condition type specifies a write error that occurred during an operation on a port.

(define-condition-type &i/o-closed-error &i/o-port-error
  i/o-closed-error?)

A condition of this type specifies that an operation tried to operate on a closed port under the assumption that it is open.

(define-condition-type &i/o-filename-error &i/o-error
  i/o-filename-error?
  (filename i/o-error-filename))

This condition type specifies an I/O error that occurred during an operation on a named file. Condition objects belonging to this type must specify a file name in the filename field.

(define-condition-type &i/o-malformed-filename-error &i/o-filename-error
  i/o-malformed-filename-error?)

This condition type indicates that a file name had an invalid format.

(define-condition-type &i/o-file-protection-error &i/o-filename-error
  i/o-file-protection-error?)

A condition of this type specifies that an operation tried to operate on a named file with insufficient access rights.

(define-condition-type &i/o-file-is-read-only-error &i/o-file-protection-error
  i/o-file-is-read-only-error?)

A condition of this type specifies that an operation tried to operate on a named read-only file under the assumption that it is writeable.

(define-condition-type &i/o-file-already-exists-error &i/o-filename-error
  i/o-file-already-exists-error?)

A condition of this type specifies that an operation tried to operate on an existing named file under the assumption that it does not exist.

(define-condition-type &i/o-no-such-file-error &i/o-filename-error
  i/o-no-such-file-error?)

A condition of this type specifies that an operation tried to operate on an non-existent named file under the assumption that it exists.

(define-condition-type &read-error &error
  read-error?
  (line read-error-line)
  (column read-error-column)
  (position read-error-position)
  (span read-error-span))

A condition of this type specifies that a parse error happened during a read operation. The fields give more information about the nature of the error. However, a Scheme implementation is not obliged to actually provide any information in any of the fields: each field value may be #f in that case.

The field values that are not #f must contain the following kinds of information:

Standard Procedures

The procedures listed in the following table must raise conditions of the subtype(s) defined above as appropriate:

call-with-input-file &i/o-filename-error
call-with-output-file &i/o-filename-error
with-input-from-file &i/o-filename-error
with-output-to-file &i/o-filename-error
output-input-file &i/o-filename-error
output-output-file &i/o-filename-error
close-output-port &i/o-write-error
read &i/o-read-error
&read-error
read-char &i/o-read-error
peek-char &i/o-read-error
write &i/o-write-error
display &i/o-write-error
newline &i/o-write-error
write-char &i/o-write-error

Read must raise a &i/o-read-error condition if a read error occurs, and a &read-error condition if a parsing error occurs.

References

Copyright

Copyright (C) Michael Sperber (2002). All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Editor: Francisco Solsona