neo_doxygen: Enhance error handling in shell scripts.
authorJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Fri, 24 Oct 2014 18:50:23 +0000 (14:50 -0400)
committerJean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>
Tue, 4 Nov 2014 17:14:12 +0000 (12:14 -0500)
Also, stop `gen-all.sh` on first error.

Signed-off-by: Jean-Christophe Beaupré <jcbrinfo@users.noreply.github.com>

contrib/neo_doxygen/gen-all.sh
contrib/neo_doxygen/gen-one.sh
contrib/neo_doxygen/sh-lib/errors.sh [new file with mode: 0644]

index bb7835a..be5ff21 100755 (executable)
@@ -29,7 +29,8 @@ NX="${PWD}/../../bin/nx"
 for dir in "$1"/*; do
        if [ -d "$dir" ]; then
                if [ -f "$dir/.nx_config" ]; then
-                       ./gen-one.sh $dir
+                       # Note: gen-one.sh already prints errors.
+                       ./gen-one.sh "$dir" || exit
                fi
        fi
 done
index 86f5925..90e7d01 100755 (executable)
@@ -27,10 +27,12 @@ NEO_DOXYGEN="${PWD}/bin/neo_doxygen"
 NX="${PWD}/../../bin/nx"
 dir=$1
 
+. sh-lib/errors.sh
+
 echo "$0: Documenting \"${dir##*/}\"..."
 pushd "$dir"
-"$NEO_DOXYGEN" "${dir##*/}" "$dir/doxygen/xml" http://localhost:7474 > neo_doxygen.out \
-&& echo "$0: [done] neo_doxygen" \
-&& "$NX" neo doc "${dir##*/}" \
-&& echo "$0: [done] nx"
+try "$NEO_DOXYGEN" "${dir##*/}" "$dir/doxygen/xml" http://localhost:7474 > neo_doxygen.out
+try echo "$0: [done] neo_doxygen"
+try "$NX" neo doc "${dir##*/}"
+try echo "$0: [done] nx"
 popd
diff --git a/contrib/neo_doxygen/sh-lib/errors.sh b/contrib/neo_doxygen/sh-lib/errors.sh
new file mode 100644 (file)
index 0000000..dc5335b
--- /dev/null
@@ -0,0 +1,46 @@
+#! /bin/bash
+
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Error handling.
+
+# The program’s name.
+prog_name=$0
+
+# Run the specified command and exit in case of error.
+function try {
+       "$@"
+       local status=$?
+       if [ $status -ne 0 ]; then
+               >&2 echo "${prog_name}: Error: \`$1\` failed with exit status ${status}."
+               trace
+               exit "$status"
+       fi
+       return 0
+}
+
+# Print the stack trace.
+function trace {
+       local frame=0
+       >&2 caller $frame
+       local has_next=$?
+       while [ $has_next = 0 ]; do
+               ((frame++));
+               >&2 caller $frame
+               has_next=$?
+       done
+       >&2 echo "---"
+       return 0
+}