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

🚀 [Feature]: expose the underlying FastHTTP upgrade error #642

Open
3 tasks done
mickaelvieira opened this issue Jun 17, 2023 · 0 comments
Open
3 tasks done

🚀 [Feature]: expose the underlying FastHTTP upgrade error #642

mickaelvieira opened this issue Jun 17, 2023 · 0 comments
Labels
✏️ Feature New feature or request

Comments

@mickaelvieira
Copy link

Feature Description

Hi everyone,

Debugging websocket connection errors can be quite difficult in some scenarios and I was wondering whether it would be possible to expose the underlying Upgrade error.

I may be wrong but it does not seem to be possible to retrieve the error returned by the FastHTTP Upgrade function as the middleware always returns a ErrUpgradeRequired error.

https://github.com/gofiber/contrib/blob/main/websocket/websocket.go#L132

The idea is to add a new struct to wrap the original error

type UpgradeError struct {
	reason error
}

func (e *UpgradeError) Error() string {
	return fiber.ErrUpgradeRequired.Error()
}

func (e *UpgradeError) Unwrap() error {
	return e.reason
}

// ...

if err := upgrader.Upgrade(c.Context(), func(fconn *websocket.Conn) {
	conn.Conn = fconn
	defer releaseConn(conn)
	handler(conn)
}); err != nil { // Upgrading required
	return &UpgradeError{reason: err}
}

Users can then wrap the middleware with their own error handler

handleErrors(websocket.New(func(c *websocket.Conn) {})
func handleErrors(handler fiber.Handler) fiber.Handler {
	return func(c *fiber.Ctx) error {
		err := handler(c)

		var upgradeError *UpgradeError
		if errors.As(err, &upgradeError ) {
			lg.Println(upgradeError.Unwrap())
		}
		return err
	}
}

Hopefully my explanation makes sense. I'm happy to submit a pull request if you find this useful.

Thanks for the great work on fiber!

Additional Context (optional)

No response

Code Snippet (optional)

package main

import "github.com/gofiber/contrib/%package%"

func main() {
  // Steps to reproduce
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my suggestion prior to opening this one.
  • I understand that improperly formatted feature requests may be closed without explanation.
@mickaelvieira mickaelvieira added the ✏️ Feature New feature or request label Jun 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant