Last active
August 29, 2015 14:11
-
-
Save fntneves/51d63fc56107144a3cc0 to your computer and use it in GitHub Desktop.
Pretty errors function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Print pretty errors | |
# Usage: pretty_error <type> <message> <name> | |
pretty_error() | |
{ | |
# Text colors | |
COLOR_NONE='\033[0m' | |
COLOR_RED='\033[0;31m' | |
COLOR_GREEN='\033[0;32m' | |
COLOR_YELLOW='\033[0;33m' | |
# Flow control variables | |
fatal=0 | |
case "${1}" in | |
error) | |
COLOR=$COLOR_RED | |
fatal=1 | |
;; | |
success) | |
COLOR=$COLOR_GREEN | |
;; | |
message) | |
COLOR=$COLOR_YELLOW | |
;; | |
*) | |
COLOR=$COLOR_NONE | |
esac | |
name="${3:-SCRIPT}" | |
message="${2}" | |
printf "[$name] ${COLOR}%s${COLOR_NONE}\n" "$message" | |
if [ $fatal -eq 1 ]; then | |
exit 1 | |
fi | |
} | |
pretty_error "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment