Standard library · System and tools

os/error

The last error from a file or process operation: its code, its message and the path that failed.

import os.error

When an operation in the fs.file, fs.dir, os.process or os.temp packages fails, the detail is recorded here. After any operation that can fail, ask HasError and then Last to find out what happened.

The plain functions of the os package do not record errors here; they only return a code. This package is a different thing from the general-purpose error package.

Types

OSError

struct OSError
The detail of an error.
NameTypeDescription
codeint = 0The error code; one of the constants above.
messagestr = ""The error message, in English.
operationstr = ""The name of the function that failed.
pathstr = ""The path that had the problem.
system_codeint = 0The operating system's own error code (errno), where it is available.
os-error-Last.salam
import os.error
import fs.file

func main:
    error.Clear()
    println error.HasError()
    file.Delete("a file that does not exist.txt")
    if error.HasError():
        e := error.Last()
        println e.code == error.FileNotFound, e.operation, e.message
        println error.LastCode(), error.LastMessage()
    end
    error.Set(error.Timeout, "time ran out", "waiting", "", 0)
    println error.LastCode() == error.Timeout, error.LastMessage()
    println error.None, error.PermissionDenied, error.AlreadyExists, error.InvalidPath, error.NotSupported, error.IOError, error.ProcessFailed
end
Outputfalse true Delete delete failed 1 delete failed true time ran out 0 2 3 4 5 6 8

Constants and variables

None

const None: i32 = 0
No error.

The example for this item is shown with the ones above: os-error-Last.salam

FileNotFound

const FileNotFound: i32 = 1
The file or directory was not found.

The example for this item is shown with the ones above: os-error-Last.salam

PermissionDenied

const PermissionDenied: i32 = 2
Access is not allowed.

The example for this item is shown with the ones above: os-error-Last.salam

AlreadyExists

const AlreadyExists: i32 = 3
It is already there.

The example for this item is shown with the ones above: os-error-Last.salam

InvalidPath

const InvalidPath: i32 = 4
The path is not valid.

The example for this item is shown with the ones above: os-error-Last.salam

NotSupported

const NotSupported: i32 = 5
This system does not support the operation.

The example for this item is shown with the ones above: os-error-Last.salam

IOError

const IOError: i32 = 6
A read or write error.

The example for this item is shown with the ones above: os-error-Last.salam

Timeout

const Timeout: i32 = 7
Time ran out.

The example for this item is shown with the ones above: os-error-Last.salam

ProcessFailed

const ProcessFailed: i32 = 8
Running the process failed.

The example for this item is shown with the ones above: os-error-Last.salam

Functions

Set

func Set(code: int, message: str, operation: str, path: str, system_code: int)
Records an error, for when you are writing a package of your own and want to report through the same mechanism.

The example for this item is shown with the ones above: os-error-Last.salam

Clear

func Clear()
Clears the recorded error, before an operation whose error you want to check on its own.

The example for this item is shown with the ones above: os-error-Last.salam

Last

func Last(): OSError
The last error with all its detail.

The example for this item is shown with the ones above: os-error-Last.salam

LastCode

func LastCode(): int
The code of the last error.

The example for this item is shown with the ones above: os-error-Last.salam

LastMessage

func LastMessage(): str
The message of the last error.

The example for this item is shown with the ones above: os-error-Last.salam

HasError

func HasError(): bool
True when an error has been recorded.

The example for this item is shown with the ones above: os-error-Last.salam