Skip to content

Commit

Permalink
Add file_stream_error.describe() function (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Jones authored and richard-viney committed Sep 8, 2024
1 parent 9279827 commit 2df6de8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/file_streams/file_stream_error.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,60 @@ pub type FileStreamError {
/// UTF-8 text file.
InvalidUnicode
}

/// Returns a human-readable description of a file stream error.
///
pub fn describe(error: FileStreamError) -> String {
case error {
Eacces -> "Permission denied"
Eagain -> "Resource temporarily unavailable"
Ebadf -> "Bad file number"
Ebadmsg -> "Bad message"
Ebusy -> "File busy"
Edeadlk -> "Resource deadlock avoided"
Edeadlock -> "File locking deadlock error"
Edquot -> "Disk quota exceeded"
Eexist -> "File already exists"
Efault -> "Bad address in system call argument"
Efbig -> "File too large"
Eftype -> "Inappropriate file type or format"
Eintr -> "Interrupted system call"
Einval -> "Invalid argument"
Eio -> "I/O error"
Eisdir -> "Illegal operation on a directory"
Eloop -> "Too many levels of symbolic links"
Emfile -> "Too many open files"
Emlink -> "Too many links"
Emultihop -> "Multihop attempted"
Enametoolong -> "Filename too long"
Enfile -> "File table overflow"
Enobufs -> "No buffer space available"
Enodev -> "No such device"
Enoent -> "No such file or directory"
Enolck -> "No locks available"
Enolink -> "Link has been severed"
Enomem -> "Not enough memory"
Enospc -> "No space left on device"
Enosr -> "No stream resources"
Enostr -> "Not a stream"
Enosys -> "Function not implemented"
Enotblk -> "Block device required"
Enotdir -> "Not a directory"
Enotsup -> "Operation not supported"
Enxio -> "No such device or address"
Eopnotsupp -> "Operation not supported on socket"
Eoverflow -> "Value too large to be stored in data type"
Eperm -> "Permission denied due to file ownership"
Epipe -> "Broken pipe"
Erange -> "Result too large"
Erofs -> "Read-only file system"
Espipe -> "Invalid seek"
Esrch -> "No such process"
Estale -> "Stale remote file handle"
Etxtbsy -> "Text file busy"
Exdev -> "Cross-domain link"
Eof -> "End of file stream"
NoTranslation(..) -> "Unable to convert encoding"
InvalidUnicode -> "Invalid bytes for Unicode encoding"
}
}
5 changes: 5 additions & 0 deletions test/file_streams_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,8 @@ pub fn set_encoding_test() {
simplifile.delete(tmp_file_name)
|> should.equal(Ok(Nil))
}

pub fn describe_test() {
file_stream_error.describe(file_stream_error.Eacces)
|> should.equal("Permission denied")
}

0 comments on commit 2df6de8

Please sign in to comment.