Skip to content

Instantly share code, notes, and snippets.

@onegentig
Last active April 8, 2025 18:53
Show Gist options
  • Save onegentig/4202d3ae32e162def48a30218e231e91 to your computer and use it in GitHub Desktop.
Save onegentig/4202d3ae32e162def48a30218e231e91 to your computer and use it in GitHub Desktop.
/**
* SYSEXITS.TS -- exit status codes for system programs
*
* This TypeScript import attempts to categorise possible
* error exit statuses for system programs. It is heavily
* based on standard C header sysexits.h, practically being
* a TypeScript re-implementation of it.
*
* Unlike the sysexits.h, this file’s constants have EXIT_
* prefix, rather than EX_, adapted from stdlib.h header.
* Similarly, constants EXIT_SUCCESS & EXIT_FAILURE were
* copied from stdlib.h header.
*
* Almost all comments and intents are copied from the
* sysexits.h header. Some names and comments were changed
* to better fit TypeScript style and modern programming
* practices.
*
* ----------------------------------------------------------------
*
* Original licence of sysexits.h:
*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)sysexits.h 8.1 (Berkeley) 6/2/93
*
* ----------------------------------------------------------------
*
* Copyright (c) 2025 onegen.dev
*
*/
/**
* Successful termination
* @description The command completed successfully (stdlib-style).
*/
export const EXIT_SUCCESS = 0;
/**
* Successful termination
* @description The command completed successfully (sysexits-style).
*/
export const EXIT_OK = 0;
/**
* General error / unsuccessful termination
* @description The command failed to complete successfully
* without a more specific error code (stdlib-style).
*/
export const EXIT_FAILURE = 1;
/**
* General error / unsuccessful termination
* @description The command failed to complete successfully
* without a more specific error code (shortened version).
*/
export const EXIT_ERR = 1;
/**
* Usage error
* @description The command was used incorrectly, e.g. with the wrong
* number of arguments, a bad flag, a bad syntax in a parameter, etc.
*/
export const EXIT_CLIERR = 64;
/**
* Data format error
* @description The input data was incorrect in some way.
* This should only be used for user’s data & not system files.
*/
export const EXIT_DATAERR = 65;
/**
* Cannot open input
* @description An input file (not a system file) did not exist
* or was not readable.
*/
export const EXIT_NOINPUT = 66;
/**
* Addressee unknown
* @description The user specified did not exist.
*/
export const EXIT_NOUSER = 67;
/**
* Host name unknown
* @description The host specified did not exist.
*/
export const EXIT_NOHOST = 68;
/**
* Service unavailable
* @description A service is unavailable. This can occur if a support program
* or file does not exist. This can also be used as a catchall message
* when something you wanted to do doesn’t work, but you don’t know why.
*/
export const EXIT_UNAVAILABLE = 69;
/**
* Internal software error
* @description An internal software error has been detected.
* This should be limited to non-operating system related errors.
*/
export const EXIT_SWERR = 70;
/**
* System error
* @description An operating system error has been detected.
* This is a catchall for system errors, not user errors, e.g.
* "can’t fork", "cannot create pipe", getuid returning a user
* that does not exist in the passwd file, and the like.
*/
export const EXIT_OSERR = 71;
/**
* Critical system file missing
* @description Some important system file does not exist,
* cannot be opened, or has some sort of error.
*/
export const EXIT_NOSYSFILE = 72;
/**
* Cannot create output file
* @description A (user specified) input file cannot be opened.
*/
export const EXIT_CANTCREATE = 73;
/**
* Input/output error
* @description An error occurred while doing I/O on some file.
*/
export const EXIT_IOERR = 74;
/**
* Temporary failure
* @description Temporary failure, indicating something that
* is not really an error. It should be used for errors where
* the user is invited to retry.
*/
export const EXIT_TEMPERR = 75;
/**
* Remote protocol error
* @description The remote system returned something that
* was "not possible" during a protocol exchange.
*/
export const EXIT_PROTOCOLERR = 76;
/**
* Permission denied
* @description An attempted operation failed due to lack of
* required permissions. This is not intended for file system
* issues (use {@link EXIT_NOINPUT} or {@link EXIT_CANTCREATE}),
* but rather for higher-level permissions.
*/
export const EXIT_NOPERM = 77;
/**
* Configuration error
* @description The user’s configuration is incorrect.
*/
export const EXIT_CFGERR = 78;
/** Converts a Deno.error to an error code */
export function errToCode(err: Error): number {
if (!('name' in err)) {
return EXIT_ERR;
}
switch (err.name) {
case 'AddrInUse':
return EXIT_UNAVAILABLE;
case 'AddrNotAvailable':
return EXIT_UNAVAILABLE;
case 'AlreadyExists':
return EXIT_CANTCREATE;
case 'BadResource':
return EXIT_IOERR;
case 'BrokenPipe':
return EXIT_IOERR;
case 'Busy':
return EXIT_TEMPERR;
case 'ConnectionAborted':
return EXIT_PROTOCOLERR;
case 'ConnectionRefused':
return EXIT_UNAVAILABLE;
case 'ConnectionReset':
return EXIT_PROTOCOLERR;
case 'FilesystemLoop':
return EXIT_NOSYSFILE;
case 'Http':
return EXIT_PROTOCOLERR;
case 'Interrupted':
return EXIT_TEMPERR;
case 'InvalidData':
return EXIT_DATAERR;
case 'IsADirectory':
return EXIT_DATAERR;
case 'NetworkUnreachable':
return EXIT_UNAVAILABLE;
case 'NotADirectory':
return EXIT_DATAERR;
case 'NotCapable':
return EXIT_UNAVAILABLE;
case 'NotConnected':
return EXIT_PROTOCOLERR;
case 'NotFound':
return EXIT_NOINPUT;
case 'NotSupported':
return EXIT_SWERR;
case 'PermissionDenied':
return EXIT_NOPERM;
case 'TimedOut':
return EXIT_TEMPERR;
case 'UnexpectedEof':
return EXIT_DATAERR;
case 'WouldBlock':
return EXIT_TEMPERR;
case 'WriteZero':
return EXIT_IOERR;
default:
return EXIT_ERR;
}
return EXIT_ERR;
}
/**
* SYSEXITS.TS -- exit status codes for system programs
*
* This TypeScript import attempts to categorise possible
* error exit statuses for system programs. It is heavily
* based on standard C header sysexits.h, practically being
* a TypeScript re-implementation of it.
*
* Unlike the sysexits.h, this file’s constants have EXIT_
* prefix, rather than EX_, adapted from stdlib.h header.
* Similarly, constants EXIT_SUCCESS & EXIT_FAILURE were
* copied from stdlib.h header.
*
* Almost all comments and intents are copied from the
* sysexits.h header. Some names and comments were changed
* to better fit TypeScript style and modern programming
* practices.
*
* ----------------------------------------------------------------
*
* Original licence of sysexits.h:
*
* Copyright (c) 1987, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)sysexits.h 8.1 (Berkeley) 6/2/93
*
* ----------------------------------------------------------------
*
* Copyright (c) 2025 onegen.dev
*
*/
/**
* Successful termination
* @description The command completed successfully (stdlib-style).
*/
export const EXIT_SUCCESS = 0;
/**
* Successful termination
* @description The command completed successfully (sysexits-style).
*/
export const EXIT_OK = 0;
/**
* General error / unsuccessful termination
* @description The command failed to complete successfully
* without a more specific error code (stdlib-style).
*/
export const EXIT_FAILURE = 1;
/**
* General error / unsuccessful termination
* @description The command failed to complete successfully
* without a more specific error code (shortened version).
*/
export const EXIT_ERR = 1;
/**
* Usage error
* @description The command was used incorrectly, e.g. with the wrong
* number of arguments, a bad flag, a bad syntax in a parameter, etc.
*/
export const EXIT_CLIERR = 64;
/**
* Data format error
* @description The input data was incorrect in some way.
* This should only be used for user’s data & not system files.
*/
export const EXIT_DATAERR = 65;
/**
* Cannot open input
* @description An input file (not a system file) did not exist
* or was not readable.
*/
export const EXIT_NOINPUT = 66;
/**
* Addressee unknown
* @description The user specified did not exist.
*/
export const EXIT_NOUSER = 67;
/**
* Host name unknown
* @description The host specified did not exist.
*/
export const EXIT_NOHOST = 68;
/**
* Service unavailable
* @description A service is unavailable. This can occur if a support program
* or file does not exist. This can also be used as a catchall message
* when something you wanted to do doesn’t work, but you don’t know why.
*/
export const EXIT_UNAVAILABLE = 69;
/**
* Internal software error
* @description An internal software error has been detected.
* This should be limited to non-operating system related errors.
*/
export const EXIT_SWERR = 70;
/**
* System error
* @description An operating system error has been detected.
* This is a catchall for system errors, not user errors, e.g.
* "can’t fork", "cannot create pipe", getuid returning a user
* that does not exist in the passwd file, and the like.
*/
export const EXIT_OSERR = 71;
/**
* Critical system file missing
* @description Some important system file does not exist,
* cannot be opened, or has some sort of error.
*/
export const EXIT_NOSYSFILE = 72;
/**
* Cannot create output file
* @description A (user specified) input file cannot be opened.
*/
export const EXIT_CANTCREATE = 73;
/**
* Input/output error
* @description An error occurred while doing I/O on some file.
*/
export const EXIT_IOERR = 74;
/**
* Temporary failure
* @description Temporary failure, indicating something that
* is not really an error. It should be used for errors where
* the user is invited to retry.
*/
export const EXIT_TEMPERR = 75;
/**
* Remote protocol error
* @description The remote system returned something that
* was "not possible" during a protocol exchange.
*/
export const EXIT_PROTOCOLERR = 76;
/**
* Permission denied
* @description An attempted operation failed due to lack of
* required permissions. This is not intended for file system
* issues (use {@link EXIT_NOINPUT} or {@link EXIT_CANTCREATE}),
* but rather for higher-level permissions.
*/
export const EXIT_NOPERM = 77;
/**
* Configuration error
* @description The user’s configuration is incorrect.
*/
export const EXIT_CFGERR = 78;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment