Use CreateAPIErrReader for error_page middleware

This commit is contained in:
joncrangle
2023-12-14 21:50:48 -05:00
parent b8fb930987
commit e8255bd3a7
5 changed files with 49 additions and 15 deletions

View File

@@ -2,10 +2,14 @@ package handlers
import (
"embed"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"strings"
"github.com/everywall/ladder/proxychain/responsemodifiers/api"
"github.com/gofiber/fiber/v2"
)
@@ -20,12 +24,35 @@ func RenderErrorPage() fiber.Handler {
}
return func(c *fiber.Ctx) error {
if err := c.Next(); err != nil {
c.Response().SetStatusCode(500)
errReader := api.CreateAPIErrReader(err)
errMessageBytes, err := io.ReadAll(errReader)
if err != nil {
return err
}
var errMsg api.Error
if err := json.Unmarshal(errMessageBytes, &errMsg); err != nil {
return err
}
if strings.Contains(c.Get("Accept"), "text/plain") {
c.Set("Content-Type", "text/plain")
return c.SendString(errMsg.Error.Message)
}
if strings.Contains(c.Get("Accept"), "text/html") {
c.Set("Content-Type", "text/html")
tmpl.Execute(c.Response().BodyWriter(), err.Error())
tmpl.Execute(c.Response().BodyWriter(), fiber.Map{
"Status": http.StatusText(c.Response().StatusCode()) + ": " + fmt.Sprint(c.Response().StatusCode()),
"Message": errMsg.Error.Message,
"Type": errMsg.Error.Type,
"Cause": errMsg.Error.Cause,
})
return nil
}
return c.SendString(err.Error())
c.Set("Content-Type", "text/json")
return c.JSON(errMsg)
}
return err
}

View File

@@ -214,7 +214,14 @@
<div class="flex flex-col space-y-3">
<h1>Error</h1>
<div class="my-4"></div>
<code class="p-4 mx-auto text-red-500 dark:text-red-400"> {{.}} </code>
<pre
class="mx-auto px-6 py-4 space-y-3 whitespace-normal break-all text-red-500 dark:text-red-400"
>
<div><span class="underline">{{.Status}}</span></div>
<div>{{.Message}}</div>
<div><span class="underline">Type</span>:&nbsp;{{.Type}}</div>
<div><span class="underline">Cause</span>:&nbsp;{{.Cause}}</div>
</pre>
</div>
<footer class="mx-4 my-2 text-center text-slate-600 dark:text-slate-400">

File diff suppressed because one or more lines are too long