Crashes are now saved in the database

This commit is contained in:
2019-11-04 16:34:00 +09:00
parent 604dc3c755
commit 2e9ad4bf6f
11 changed files with 125 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import (
"strings"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/arn"
)
// Recover recovers from panics and shows them as the response body.
@ -27,11 +28,28 @@ func Recover(next aero.Handler) aero.Handler {
}
stack := make([]byte, 4096)
length := runtime.Stack(stack, true)
length := runtime.Stack(stack, false)
stackString := string(stack[:length])
fmt.Fprint(os.Stderr, stackString)
message := err.Error() + "<br><br>" + strings.ReplaceAll(stackString, "\n", "<br>")
// Save crash in database
crash := &arn.Crash{
Error: err.Error(),
Stack: stackString,
}
crash.ID = arn.GenerateID("Crash")
crash.Created = arn.DateTimeUTC()
user := arn.GetUserFromContext(ctx)
if user != nil {
crash.CreatedBy = user.ID
}
crash.Save()
// Send HTML
message := "<div class='crash'>" + err.Error() + "<br><br>" + strings.ReplaceAll(stackString, "\n", "<br>") + "</div>"
_ = ctx.Error(http.StatusInternalServerError, message)
}()