Do stack trace + more
[errhandle.git] / errexit-function
1 #!/bin/bash
2 # Copyright (C) 2015 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 errexit() {
17 # usage: errexit [EXIT_CODE] [MESSAGE]
18 exec >&2
19 code=1
20 if [[ $@ ]]; then
21 if [[ ${1/[^0-9]/} == "$1" ]]; then
22 code=$1
23 if [[ $2 ]]; then
24 echo "$2"
25 fi
26 else
27 echo "$0: $1"
28 fi
29 fi
30 echo "${BASH_SOURCE[1]}:${BASH_LINENO[0]}"
31 bash-trace 2
32 echo "$0: exiting with code $code"
33 exit $err
34 }