Skip to content
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

Closed
1995parham opened this issue Feb 12, 2020 · 11 comments
Closed

🤔 Is there any way for handling JSON error? #126

1995parham opened this issue Feb 12, 2020 · 11 comments

Comments

@1995parham
Copy link

Question description
I have used gofiber for creating a REST API with JSON. The JSON function returns error so I must handle it every time in responses. Is there any better way of doing this?

Code snippet (optional)

package main

import "github.com/gofiber/fiber"

func main() {
  app := fiber.New()
  app.Get("/json", func (c *fiber.Ctx) {
    if err := c.Status(200).JSON("Hello World"); err != nil {
      panic(err)
    }
  })
}
@welcome
Copy link

welcome bot commented Feb 12, 2020

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template!

@koddr
Copy link
Contributor

koddr commented Feb 12, 2020

@1995parham thx for issue.

We're working around easy way to error handling right now on Fiber. Stay tuned! 😉

@1995parham
Copy link
Author

@koddr Thanks for your response 🙌 I will wait.

@Fenny
Copy link
Member

Fenny commented Feb 12, 2020

@1995parham, thanks for submitting your issue.

Fiber is really flexible, you could create your own error handling logic.
The status code defaults to 200 unless changed, you can remove that.

In Fiber v1.5.0 you can pass errors in the Next method.

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.
Passing the err into c.Next(err) would be nice.
So you can retrieve the error from the Ctx later on with c.Error() for example.

PS: We added Next(err) & app.Recover(handler) & c.Error() #128

@1995parham
Copy link
Author

@Fenny I agree with your solution, we can return an error from the handler. If you want, I can create a PR for it.

@Fenny
Copy link
Member

Fenny commented Feb 12, 2020

@1995parham, PR's are always welcome 👍
I have time to work on this in the afternoon ☕

@1995parham
Copy link
Author

I will create PR so we can work on it 🙌

@Fenny
Copy link
Member

Fenny commented Feb 12, 2020

@1995parham, me and @koddr are actually discussing some kind of recover feature.
This has nothing to do with the c.Next(err), but we would like to hear your thoughts on this.

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)
}

@1995parham
Copy link
Author

@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.

@Fenny Fenny mentioned this issue Feb 12, 2020
@Fenny
Copy link
Member

Fenny commented Feb 12, 2020

@1995parham 'v1.5.0' is released, docs coming soon.

https://fiber.wiki/application#recover
https://fiber.wiki/context#error
https://fiber.wiki/context#next

@Fenny Fenny closed this as completed Feb 12, 2020
@1995parham
Copy link
Author

@Fenny, sorry for bothering you again. I have checked the new API, and it was good. But is there any way to return an error from the handler, and it automatically passed to the Next method? Like handlers of echo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants