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

Fix NewErrors() and Improve NewError() #1728

Merged
merged 9 commits into from Jan 27, 2022

Conversation

balcieren
Copy link
Member

@balcieren balcieren commented Jan 21, 2022

If error message's type happens interface{}, It can return multiple error strings etc.

NewErrors()

func main() {
	app := New(fiber.Config{
		ErrorHandler: func(c *fiber.Ctx, err error) error {
			code := fiber.StatusInternalServerError
			var messages interface{}

			if e, ok := err.(*fiber.Error); ok {
				code = e.Code
				messages = e.Message
			}

			return c.Status(code).JSON(fiber.Map{
				"status_code": code,
				"status_text": utils.StatusMessage(code),
				"messages":    messages,
			})
		},
	})

	app.Get("/", func(c *fiber.Ctx) error {
		return NewErrors(fiber.StatusBadRequest, "validatien error 1", "validation error 2")
	})

	app.Listen(":3000")
}

JSON is going to be

{
  "messages": [
    "validatien error 1",
    "validation error 2"
  ],
  "status_code": 400,
  "status_text": "Bad Request"
}

Different Example

func main() {
	app := New(fiber.Config{
		ErrorHandler: func(c *fiber.Ctx, err error) error {
			code := fiber.StatusInternalServerError
			var messages interface{}

			if e, ok := err.(*fiber.Error); ok {
				code = e.Code
				messages = e.Message
			}

			return c.Status(code).JSON(fiber.Map{
				"status_code": code,
				"status_text": utils.StatusMessage(code),
				"messages":    messages,
			})
		},
	})

	app.Get("/", func(c *fiber.Ctx) error {
		return NewErrors(StatusBadRequest,
			fiber.Map{
				"type":        "validation",
				"description": "password must be longer than 6 characters",
			},
			fiber.Map{
				"type":        "validation",
				"description": "invalid e-mail",
			})
	})

	app.Listen(":3000")
}

JSON is going to be

{
  "messages": [
    {
      "description": "password must be longer than 6 characters",
      "type": "validation"
    },
    {
      "description": "invalid e-mail",
      "type": "validation"
    }
  ],
  "status_code": 400,
  "status_text": "Bad Request"
}

@balcieren balcieren changed the title Fix and Improve for NewErrors() Fix NewErrors() and Improve NewError() Jan 21, 2022
@vecpeng
Copy link
Contributor

vecpeng commented Jan 22, 2022

It seems good but the only problem is that the type of message has been changed, which may cause backward compatibility issues.
Maybe we should wait maintainers' review.
@ReneWerner87 @efectn @hi019 what do you think?

@balcieren
Copy link
Member Author

balcieren commented Jan 22, 2022

It seems good but the only problem is that the type of message has been changed, which may cause backward compatibility issues. Maybe we should wait maintainers' review. @ReneWerner87 @efectn @hi019 what do you think?

Echo also uses interface{} for message

app.go Show resolved Hide resolved
@balcieren
Copy link
Member Author

balcieren commented Jan 26, 2022

@ReneWerner87 I think it is not big update and all checks have passed. What do you think ?

@ReneWerner87
Copy link
Member

ReneWerner87 commented Jan 26, 2022

I look at it today, had the week other tasks to do

@ReneWerner87 ReneWerner87 merged commit 7cf1886 into gofiber:master Jan 27, 2022
14 checks passed
@welcome
Copy link

welcome bot commented Jan 27, 2022

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@balcieren
Copy link
Member Author

balcieren commented Jan 27, 2022

@ReneWerner87 I have updated doc, PR

@ReneWerner87
Copy link
Member

ReneWerner87 commented Jan 27, 2022

Thx

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

Successfully merging this pull request may close these issues.

4 participants