-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🤔 Is there any way for handling JSON error? #126
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! |
@1995parham thx for issue. We're working around easy way to error handling right now on Fiber. Stay tuned! 😉 |
@koddr Thanks for your response 🙌 I will wait. |
@1995parham, thanks for submitting your issue. Fiber is really flexible, you could create your own In Fiber package main
import "github.com/gofiber/fiber"
func main() {
app := fiber.New()
app.Post("/api/register", func (c *fiber.Ctx) {
if err := c.JSON(&User); err != nil {
c.Next(err)
}
})
app.Get("/api/user", func (c *fiber.Ctx) {
if err := c.JSON(&User); err != nil {
c.Next(err)
}
})
app.Put("/api/update", func (c *fiber.Ctx) {
if err := c.JSON(&User); err != nil {
c.Next(err)
}
})
app.Use("/api", func(c *fiber.Ctx) {
c.Set("Content-Type", "application/json")
c.Status(500).Send(c.Error())
})
app.Listen(1337)
} We are working on a better way to handle global errors. PS: We added |
@Fenny I agree with your solution, we can return an error from the handler. If you want, I can create a PR for it. |
@1995parham, PR's are always welcome 👍 |
I will create PR so we can work on it 🙌 |
@1995parham, me and @koddr are actually discussing some kind of package main
import (
"./fiber"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) {
panic("I panic")
})
// app.Panic, app.Error // not sure yet
app.Recover(func(c *fiber.Ctx) {
fmt.Println(c.Error()) // => "I panic"
c.SendStatus(500) // => 500 "Internal Server Error"
})
app.Listen(3000)
} |
@Fenny I think to recover from panic in handlers is an excellent feature, and it would be useful in long-running applications. The proposed syntax is simple and useful. |
@1995parham 'v1.5.0' is released, docs coming soon. https://fiber.wiki/application#recover |
Question description
I have used
gofiber
for creating a REST API with JSON. TheJSON
function returns error so I must handle it every time in responses. Is there any better way of doing this?Code snippet (optional)
The text was updated successfully, but these errors were encountered: