Use CreateAPIErrReader for error_page middleware
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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>: {{.Type}}</div>
|
||||
<div><span class="underline">Cause</span>: {{.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
@@ -390,18 +390,18 @@ func (chain *ProxyChain) SetDebugLogging(isDebugMode bool) *ProxyChain {
|
||||
// this will prevent Execute from firing and reset the state
|
||||
// returns the initial error enriched with context
|
||||
func (chain *ProxyChain) abort(err error) error {
|
||||
// defer chain._reset()
|
||||
defer chain._reset()
|
||||
chain.abortErr = err
|
||||
chain.Context.Response().SetStatusCode(500)
|
||||
var e error
|
||||
if chain.Request.URL != nil {
|
||||
e = fmt.Errorf("ProxyChain error for '%s': %s", chain.Request.URL.String(), err.Error())
|
||||
} else {
|
||||
e = fmt.Errorf("ProxyChain error: '%s'", err.Error())
|
||||
}
|
||||
// chain.Context.Response().SetStatusCode(500)
|
||||
// var e error
|
||||
// if chain.Request.URL != nil {
|
||||
// e = fmt.Errorf("ProxyChain error for '%s': %s", chain.Request.URL.String(), err.Error())
|
||||
// } else {
|
||||
// e = fmt.Errorf("ProxyChain error: '%s'", err.Error())
|
||||
// }
|
||||
// chain.Context.SendString(e.Error()) // <- RenderErrorPage middleware to render error
|
||||
log.Println(e.Error())
|
||||
return e
|
||||
// log.Println(e.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
// internal function to reset state of ProxyChain for reuse
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
kbd,
|
||||
pre,
|
||||
code {
|
||||
@apply relative whitespace-break-spaces rounded bg-slate-200 dark:bg-slate-800 py-[0.2rem] font-mono text-sm font-semibold;
|
||||
@apply relative whitespace-break-spaces break-words rounded bg-slate-200 dark:bg-slate-800 py-[0.2rem] font-mono text-sm font-semibold;
|
||||
}
|
||||
blockquote {
|
||||
@apply mt-6 border-l-2 pl-6 italic;
|
||||
|
||||
Reference in New Issue
Block a user