Skip to content

Commit

Permalink
Use CreateAPIErrReader for error_page middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
joncrangle committed Dec 15, 2023
1 parent b8fb930 commit e8255bd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
31 changes: 29 additions & 2 deletions handlers/error_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion handlers/error_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion handlers/styles.css

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions proxychain/proxychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion styles/input.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e8255bd

Please sign in to comment.