Skip to content

Commit

Permalink
improoved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirari04 committed Jan 18, 2024
1 parent 6554dda commit f6025b2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
5 changes: 4 additions & 1 deletion controllers/PlayerController.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func PlayerController(c echo.Context) error {
}
var requestValidation Request
if status, err := helpers.Validate(c, &requestValidation); err != nil {
return c.String(status, err.Error())
return c.Render(status, "error.html", echo.Map{
"Title": "Player Error",
"Error": err.Error(),
})
}

//check if requested folder exists
Expand Down
13 changes: 7 additions & 6 deletions inits/Server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,17 @@ func Server() {

app.IPExtractor = echo.ExtractIPFromXFFHeader(trustOptions...)
app.HTTPErrorHandler = func(err error, c echo.Context) {
// Status code defaults to 500
code := http.StatusInternalServerError

// Retrieve the custom status code if it's a *echo.HTTPError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}

if code == http.StatusInternalServerError {
c.Logger().Error(err)
c.Logger().Error(err)
if code == 404 {
if err := c.Render(code, "404.html", echo.Map{}); err != nil {
c.Logger().Error(err)
}
} else {
c.NoContent(code)
}
}

Expand Down
7 changes: 5 additions & 2 deletions views/404.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<title>Not Found</title>
</head>
<body>

<body style="background-color: black;color: white;">
<h1>Not Found</h1>
</body>

</html>
18 changes: 18 additions & 0 deletions views/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
</head>

<body style="background-color: black;color: white;">
<h1>{{.Title}}</h1>
<p>
{{.Error}}
</p>
</body>

</html>

0 comments on commit f6025b2

Please sign in to comment.