/
error_handler
executable file
·108 lines (99 loc) · 3.91 KB
/
error_handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
## This file is part of Whonix.
## Copyright (C) 2012 - 2014 Patrick Schleizer <adrelanos@riseup.net>
## See the file COPYING for copying conditions.
error_handler() {
local exit_code="$?"
BUG="1"
local error_cause error_text
error_text="$1"
if [ "$error_text" = "" ]; then
error_cause="$FUNCNAME signal ERR detected with BASH_COMMAND:
$BASH_COMMAND"
else
error_cause="$FUNCNAME called with error_text:
$error_text"
fi
if [ -d /usr/share/whonix ]; then
local MSG="<p>###############################################################################
<br></br>## $SCRIPTNAME script bug.
<br></br>## No panic. Nothing is broken. Just some rare condition has been hit.
<br></br>## Try again later. There is likely a solution for this problem.
<br></br>## Please see Whonix News, Whonix Blog and Whonix User Help Forum.
<br></br>## Please report this bug!
<br></br>##
<br></br>## who_ami: $who_ami
<br></br>## identifier: $identifier
<br></br>## exit_code: $exit_code
<br></br>## error_cause: $error_cause
<br></br>##
<br></br>## Experts only:
<br></br>## bash -x $SCRIPTNAME --verbose
<br></br>## for verbose output. Clean the output and report to Whonix developers.
<br></br>#########################################################################</p>"
else
local MSG="<p>###############################################################################
<br></br>## $SCRIPTNAME script bug.
<br></br>## No panic. Nothing is broken. Just some rare condition has been hit.
<br></br>## Try again later. There is likely a solution for this problem.
<br></br>## Please see Whonix News, Whonix Blog and Whonix User Help Forum.
<br></br>## Please report this bug!
<br></br>##
<br></br>## who_ami: $who_ami
<br></br>## identifier: $identifier
<br></br>## exit_code: $exit_code
<br></br>## error_cause: $error_cause
<br></br>##
<br></br>## Experts only:
<br></br>## bash -x $SCRIPTNAME --verbose
<br></br>## for verbose output. Clean the output and report to Whonix developers.
<br></br>#########################################################################</p>"
fi
## {{{ add/show error message
local command_v_output_exit_code
command_v_output_exit_code="0"
command -v "$output" >/dev/null || { command_v_output_exit_code="$?" ; true; };
## Check if $output command has been already defined. This is not the case,
## when the script is terminated very early.
if [ "$command_v_output_exit_code" = "0" ]; then
if [ "$TITLE" = "" ]; then
TITLE="No TITLE defined yet."
fi
$output ${output_opts[@]} --titlex "$TITLE"
$output ${output_opts[@]} --titlecli "$TITLE"
$output ${output_opts[@]} --messagex --typex "error" --message "$MSG"
$output ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
else
local stripped_msg
stripped_msg="$(/usr/lib/msgcollector/striphtml "$MSG")"
if [ "$stripped_msg" = "" ]; then
## In case striphtml failed or is not available.
echo "$MSG" >&2
else
echo "$stripped_msg" >&2
fi
fi
## }}}
## Check if ex_funct command has been already defined. This is not the case,
## when the script is terminated very early.
local command_v_output_exit_code
command_v_output_exit_code="0"
command -v "ex_funct" >/dev/null || { command_v_output_exit_code="$?" ; true; };
if [ "$command_v_output_exit_code" = "0" ]; then
## ex_funct exists. It is up to ex_funct to exit.
echo "$SCRIPTNAME: Error detected. Cleaning up... Exiting..." >&2
SIGNAL_TYPE="ERR"
ex_funct || true
echo "$SCRIPTNAME: Error! ex_funct did not exit!" >&2
echo "$SCRIPTNAME: Exiting..." >&2
if [ "$EXIT_CODE" = "" ]; then
EXIT_CODE="1"
fi
exit "$EXIT_CODE"
fi
echo "$SCRIPTNAME: Error detected. Skipping ex_funct since not yet load. Exiting..." >&2
if [ "$EXIT_CODE" = "" ]; then
EXIT_CODE="1"
fi
exit "$EXIT_CODE"
}