Skip to content
Snippets Groups Projects
Verified Commit 02b555c4 authored by Réthelyi Bálint's avatar Réthelyi Bálint :no_mouth:
Browse files

error handling

parent 2bf832f8
Branches
Tags
No related merge requests found
package web
import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"net/http"
)
func errorHandler(c *gin.Context) {
c.Next()
......@@ -10,6 +13,8 @@ func errorHandler(c *gin.Context) {
errorList[i] = e.Error()
}
if len(errorList) > 0 {
c.JSON(400, errorList)
c.HTML(http.StatusBadRequest, "error.html", gin.H{
"errors": errorList,
})
}
}
......@@ -2,36 +2,36 @@ package web
import (
"errors"
"fmt"
"git.sch.bme.hu/blintmester/keresett-a-feri/route"
"github.com/gin-gonic/gin"
"io/fs"
"net/http"
)
var Router = gin.Default()
type StationURI struct {
Name string `uri:"name" binding:"required"`
//Name string `uri:"name" binding:"required"`
Id string `uri:"uuid" binding:"required"`
}
func init() {
staticfs, e := fs.Sub(static, "static")
if e != nil {
panic(e)
}
//staticfs, e := fs.Sub(static, "static")
//if e != nil {
// panic(e)
//}
Router.Use(errorHandler)
//Router.SetHTMLTemplate(html)
Router.StaticFS("/", http.FS(staticfs))
Router.SetHTMLTemplate(html)
//for _, station := range route.Middle {
// Router.StaticFS("/" + station.Id, http.FS(staticfs))
//}
Router.GET("/", mainPage)
//Router.StaticFS("/static", http.FS(staticfs))
Router.GET("/station/:uuid", routeChooser)
}
Router.GET("/:name/:uuid", routeChooser)
func mainPage(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
}
func routeChooser(c *gin.Context) {
......@@ -41,20 +41,16 @@ func routeChooser(c *gin.Context) {
return
}
if stationUri.Name != "station" {
c.Error(errors.New("wrong path")) // TODO: better error msg
return
}
if stationUri.Id == "first" {
c.HTML(http.StatusOK, "static/index.html", nil)
fmt.Println("First station")
} else if stationUri.Id == "last" {
fmt.Println("Last station")
} else {
found := false
for _, station := range route.Middle {
if stationUri.Id == station.Id {
found = true
fmt.Println(station.Id)
break
}
}
......
......@@ -2,12 +2,13 @@ package web
import (
"embed"
"html/template"
)
//go:embed static/*
var static embed.FS
////go:embed static/*
//var static embed.FS
////go:embed templates/*
//var templates embed.FS
//
//var html = template.Must(template.ParseFS(templates, "templates/*"))
//go:embed templates/*
var templates embed.FS
var html = template.Must(template.ParseFS(templates, "templates/*"))
<!DOCTYPE html>
<html lang="hu">
<head>
<meta charset="UTF-8">
<title>Error</title>
</head>
<body>
<h1>Something fucked...</h1>
<h2>Errors:</h2>
{{- range $key, $error := .errors }}
<ul>{{ $error }}</ul>
{{- end }}
</body>
</html>
\ No newline at end of file
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment