Errno 모듈
Errno 모듈
운영체제(OS)가 오류를 만나면 보통 그 오류를 정수 에러 코드로 알려줘요. 셸에서 ls nosuch.txt 같은 명령을 내리면 "No such file or directory"라는 메시지와 함께 종료 코드 2가 남는 걸 본 적이 있을 거예요.
Ruby 인터프리터가 OS와 상호작용하다 이렇게 에러 코드(예: 2)를 받으면, 그 코드를 특정 Ruby 예외 클래스(예: Errno::ENOENT)에 연결해요:
File.open('nosuch.txt')
# => No such file or directory @ rb_sysopen - nosuch.txt (Errno::ENOENT)
이 각각의 클래스는:
- 이
Errno모듈의 중첩 클래스이고, SystemCallError클래스의 하위 클래스이며,- 하나의 에러 코드와 연결돼 있어요.
그래서 이렇게 확인할 수 있어요:
Errno::ENOENT.superclass # => SystemCallError
Errno::ENOENT::Errno # => 2
중첩 클래스들의 이름은 Errno.constants로 얻을 수 있어요:
Errno.constants.size # => 158
Errno.constants.sort.take(5) # => [:E2BIG, :EACCES, :EADDRINUSE, :EADDRNOTAVAIL, :EADV]
위에서 보듯 각 클래스와 연결된 에러 코드는 상수 값으로 얻을 수 있어요. 다만 그 값은 운영체제마다 달라질 수 있어요. 특정 운영체제에 필요 없는 클래스라면 그 값은 0이에요:
Errno::ENOENT::Errno # => 2
Errno::ENOTCAPABLE::Errno # => 0
Errno의 각 클래스는 선택적 메시지와 함께 만들 수도 있어요:
Errno::EPIPE.new # => #<Errno::EPIPE: Broken pipe>
Errno::EPIPE.new("foo") # => #<Errno::EPIPE: Broken pipe - foo>
Errno::EPIPE.new("foo", "here") # => #<Errno::EPIPE: Broken pipe @ here - foo>
SystemCallError.new도 함께 보세요.
상수
아래는 Ruby가 정의하는 오류 상수들의 일부예요. 의미와 함께 정리하면:
| 상수 | 의미 |
|---|---|
E2BIG |
"Argument list too long" (인자 목록이 너무 김) |
EACCES |
"Permission denied" (권한 거부) |
EADDRINUSE |
"Address already in use" (주소 이미 사용 중) |
EADDRNOTAVAIL |
"Address not available" (주소 사용 불가) |
EADV |
"Advertise error" |
EAFNOSUPPORT |
"Address family not supported" (주소 패밀리 미지원) |
EAGAIN |
"Resource temporarily unavailable, try again" (EWOULDBLOCK과 같은 값일 수 있음) |
EALREADY |
"Connection already in progress" (연결이 이미 진행 중) |
EBADF |
"Bad file descriptor" (잘못된 파일 디스크립터) |
EBADMSG |
"Bad message" (잘못된 메시지) |
EBUSY |
"Device or resource busy" (장치/자원 사용 중) |
ECANCELED |
"Operation canceled" (작업 취소됨) |
ECHILD |
"No child processes" |
ECONNABORTED |
"Connection aborted" (연결 중단) |
ECONNREFUSED |
"Connection refused" (연결 거부) |
ECONNRESET |
"Connection reset" (연결 리셋) |
EDEADLK |
"Resource deadlock avoided" |
EDESTADDRREQ |
"Destination address required" |
EDOM |
"Mathematics argument out of domain" |
EDQUOT |
"Disk quota exceeded" (디스크 쿼터 초과) |
EEXIST |
"File exists" (파일 존재) |
EFAULT |
"Bad address" |
EFBIG |
"File too large" (파일이 너무 큼) |
EHOSTDOWN |
"Host is down" |
EHOSTUNREACH |
"Host is unreachable" |
EIDRM |
"Identifier removed" |
EILSEQ |
"Invalid or incomplete multibyte or wide character" |
EINPROGRESS |
"Operation in progress" |
EINTR |
"Interrupted function call" |
EINVAL |
"Invalid argument" (잘못된 인자) |
EIO |
"Input/output error" |
EISCONN |
"Socket is connected" |
EISDIR |
"Is a directory" |
EKEYEXPIRED |
"Key has expired" |
EKEYREJECTED |
"Key was rejected by service" |
EKEYREVOKED |
"Key has been revoked" |
ELOOP |
"Too many levels of symbolic links" |
EMFILE |
"Too many open files" (열린 파일이 너무 많음) |
EMLINK |
"Too many links" |
EMSGSIZE |
"Message too long" |
ENAMETOOLONG |
"Filename too long" (파일명이 너무 김) |
ENETDOWN |
"Network is down" |
ENETRESET |
"Connection aborted by network" |
ENETUNREACH |
"Network unreachable" |
ENFILE |
"Too many open files in system" |
ENOBUFS |
"No buffer space available" |
ENODATA |
"No data available" |
ENODEV |
"No such device" |
ENOENT |
"No such file or directory" (그런 파일/디렉터리 없음) |
ENOEXEC |
"Exec format error" |
ENOLCK |
"No locks available" |
ENOMEM |
"Not enough space/cannot allocate memory" |
ENOMSG |
"No message of the desired type" |
ENONET |
"Machine is not on the network" |
ENOPROTOOPT |
"Protocol not available" |
ENOSPC |
"No space left on device" (장치 공간 부족) |
ENOSYS |
"Functionality not implemented" |
ENOTCONN |
"The socket is not connected" |
ENOTDIR |
"Not a directory" |
ENOTEMPTY |
"Directory not empty" |
ENOTSOCK |
"Not a socket" |
ENOTSUP |
"Operation not supported" |
ENOTTY |
"Inappropriate I/O control operation" |
ENXIO |
"No such device or address" |
EOPNOTSUPP |
"Operation not supported on socket" |
EOVERFLOW |
"Value too large to be stored in data type" |
EPERM |
"Operation not permitted" (권한 없음) |
EPFNOSUPPORT |
"Protocol family not supported" |
EPIPE |
"Broken pipe" (끊어진 파이프) |
EPROTO |
"Protocol error" |
EPROTONOSUPPORT |
"Protocol not supported" |
EPROTOTYPE |
"Protocol wrong type for socket" |
ERANGE |
"Result too large" |
EROFS |
"Read-only file system" (읽기 전용 파일시스템) |
ESPIPE |
"Illegal seek" |
ESRCH |
"No such process" |
ESTALE |
"Stale file handle" |
ETIME |
"Timer expired" |
ETIMEDOUT |
"Connection timed out" (연결 시간 초과) |
ETXTBSY |
"Text file busy" |
EUSERS |
"Too many users" |
EWOULDBLOCK |
"Operation would block" |
EXDEV |
"Invalid cross-device link" |
NOERROR |
오류 없음 |
(목록에 있는 모든 상수, 예컨대 EAUTH·EBADRPC·EHWPOISON 등 더 세부 항목도 같은 패턴으로 존재해요. 전체 목록은 Errno.constants로 확인할 수 있어요.)
더 알아보기
- SystemCallError —
Errno각 클래스가 상속하는 부모 클래스