11. uerrno

The uerrno module in MicroPython provides access to symbolic error codes for OSError exceptions.
It can also be imported as errno.
These error codes are useful for handling specific error conditions in your code.

11.1. Error codes

The code below scrolls the dictionary of error codes. help(uerrno) still prints to the REPL.
from microbit import *
import uerrno

help(uerrno)
error_codes = uerrno.errorcode
# print(error_codes)
display.scroll(str(error_codes), delay=80)
# print(uerrno.errorcode[uerrno.EINVAL])
display.scroll(str(uerrno.errorcode[uerrno.EINVAL]), delay=80)
# print(uerrno.errorcode[22])
display.scroll(str(uerrno.errorcode[22]), delay=80)
errorcode -- {1: 'EPERM', 2: 'ENOENT', 5: 'EIO',
9: 'EBADF', 11: 'EAGAIN', 12: 'ENOMEM',
13: 'EACCES', 17: 'EEXIST', 19: 'ENODEV',
21: 'EISDIR', 22: 'EINVAL', 95: 'EOPNOTSUPP',
98: 'EADDRINUSE', 103: 'ECONNABORTED',
104: 'ECONNRESET', 105: 'ENOBUFS',
107: 'ENOTCONN', 110: 'ETIMEDOUT',
111: 'ECONNREFUSED', 113: 'EHOSTUNREACH',
114: 'EALREADY', 115: 'EINPROGRESS'}

11.2. File open error

The code below scrolls a message if the file does not exist.
# Imports go at the top
from microbit import *
import uerrno

try:
    with open('non_existent_file.txt', 'r') as file:
        content = file.read()
except OSError as e:
    if e.errno == uerrno.ENOENT:
        # print("Error: File not found.")
        display.scroll("Error: File not found.", delay=80)
    else:
        # print("Error: {}".format(e))
        display.scroll(str("Error: {}".format(e)), delay=80)

11.3. Error code meanings

Here are the meanings of each error code in the provided list:
  1. EPERM (1): Operation not permitted.

  2. ENOENT (2): No such file or directory.

  3. EIO (5): Input/output error.

  4. EBADF (9): Bad file descriptor.

  5. EAGAIN (11): Try again (resource temporarily unavailable).

  6. ENOMEM (12): Out of memory.

  7. EACCES (13): Permission denied.

  8. EEXIST (17): File exists.

  9. ENODEV (19): No such device.

  10. EISDIR (21): Is a directory.

  11. EINVAL (22): Invalid argument.

  12. EOPNOTSUPP (95): Operation not supported.

  13. EADDRINUSE (98): Address already in use.

  14. ECONNABORTED (103): Software caused connection abort.

  15. ECONNRESET (104): Connection reset by peer.

  16. ENOBUFS (105): No buffer space available.

  17. ENOTCONN (107): Transport endpoint is not connected.

  18. ETIMEDOUT (110): Connection timed out.

  19. ECONNREFUSED (111): Connection refused.

  20. EHOSTUNREACH (113): No route to host.

  21. EALREADY (114): Operation already in progress.

  22. EINPROGRESS (115): Operation now in progress.