@@ -6,6 +6,7 @@ export SENTRY_PROJECT=installer
66
77jq=" docker run --rm -i sentry-self-hosted-jq-local"
88sentry_cli=" docker run --rm -v /tmp:/work -e SENTRY_ORG=$SENTRY_ORG -e SENTRY_PROJECT=$SENTRY_PROJECT -e SENTRY_DSN=$SENTRY_DSN getsentry/sentry-cli"
9+ log_path=" $basedir /$log_file "
910
1011send_envelope () {
1112 # Send envelope
@@ -25,11 +26,11 @@ generate_breadcrumb_json() {
2526send_event () {
2627 # Use traceback hash as the UUID since it is 32 characters long
2728 local event_hash=$1
28- local error_message =$2
29+ local error_msg =$2
2930 local traceback_json=$3
31+ local breadcrumbs=$4
3032 local envelope_file=" sentry-envelope-${event_hash} "
3133 local envelope_file_path=" /tmp/$envelope_file "
32- local log_path=" $basedir /$log_file "
3334 # If the envelope file exists, we've already sent it
3435 if [[ -f $envelope_file_path ]]; then
3536 echo " Looks like you've already sent this error to us, we're on it :)"
@@ -52,18 +53,15 @@ send_event() {
5253 # See https://develop.sentry.dev/sdk/event-payloads/
5354 # for details about the event payload
5455
55- # First, create the breadcrumb payload
56- # https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/
57- breadcrumbs=$( generate_breadcrumb_json | jq -s -c)
58- # Next we need the exception payload
56+ # Then we need the exception payload
5957 # https://develop.sentry.dev/sdk/event-payloads/exception/
6058 # but first we need to make the stacktrace which goes in the exception payload
6159 frames=$( echo " $traceback_json " | jq -s -c)
62- stacktrace=$( jq -n -c --argjson frames $frames ' $ARGS.named' )
60+ stacktrace=$( jq -n -c --argjson frames " $frames " ' $ARGS.named' )
6361 exception=$(
6462 jq -n -c --arg " type" Error \
65- --arg value " $error_message " \
66- --argjson stacktrace $stacktrace \
63+ --arg value " $error_msg " \
64+ --argjson stacktrace " $stacktrace " \
6765 ' $ARGS.named'
6866 )
6967 event_body=$(
@@ -170,6 +168,12 @@ cleanup() {
170168 DID_CLEAN_UP=1
171169 if [[ " $1 " != " EXIT" ]]; then
172170 set +o xtrace
171+ # Save the error message that comes from the last line of the log file
172+ error_msg=$( tail -n 1 " $log_path " )
173+ # Create the breadcrumb payload now before stacktrace is printed
174+ # https://develop.sentry.dev/sdk/event-payloads/breadcrumbs/
175+ # Use sed to remove the last line, that is reported through the error message
176+ breadcrumbs=$( generate_breadcrumb_json | sed ' $d' | jq -s -c)
173177 printf -v err ' %s' " Error in ${BASH_SOURCE[1]} :${BASH_LINENO[0]} ."
174178 printf -v cmd_exit ' %s' " '$cmd ' exited with status $retcode "
175179 printf ' %s\n%s\n' " $err " " $cmd_exit "
@@ -188,6 +192,14 @@ cleanup() {
188192 --arg lineno " $lineno " \
189193 ' {"filename": $filename, "function": $function, "lineno": $lineno|tonumber}'
190194 )
195+ # If we're in the stacktrace of the file we failed on, we can add a context line with the command run that failed
196+ if [[ $i -eq 1 ]]; then
197+ JSON=$(
198+ jq -n -c --arg cmd " $cmd " \
199+ --argjson json " $JSON " \
200+ ' $json + {"context_line": $cmd}'
201+ )
202+ fi
191203 printf -v traceback_json ' %s\n' " $traceback_json$JSON "
192204 printf -v traceback ' %s\n' " $traceback ${indent// a/ -} > $src :$funcname :$lineno "
193205 done
@@ -197,7 +209,7 @@ cleanup() {
197209 # Only send event when report issues flag is set and if trap signal is not INT (ctrl+c)
198210 if [[ " $REPORT_SELF_HOSTED_ISSUES " == 1 && " $1 " != " INT" ]]; then
199211 local event_hash=$( echo -n " $cmd_exit $traceback " | docker run -i --rm busybox md5sum | cut -d' ' -f1)
200- send_event " $event_hash " " $cmd_exit " " $traceback_json "
212+ send_event " $event_hash " " $error_msg " " $traceback_json " " $breadcrumbs "
201213 fi
202214
203215 if [[ -n " $MINIMIZE_DOWNTIME " ]]; then
0 commit comments