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 add recover call in ServeHTTP #720

Merged

Conversation

Sianao
Copy link
Contributor

@Sianao Sianao commented Jun 14, 2024

Add recovery logic in the serve-http function

Description:

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

@aryanmehrotra
Copy link
Contributor

Hey @Sianao can you describe the issue? as panic recovery is already supported by gofr.

@Sianao
Copy link
Contributor Author

Sianao commented Jun 14, 2024

such as this one

package main

import "gofr.dev/pkg/gofr"

func main() {
	// initialise gofr object
	app := gofr.New()

	// register route greet
	app.GET("/greet", func(ctx *gofr.Context) (interface{}, error) {
		panic(nil)
		return "Hello World!", nil
	})

	// Runs the server, it will listen on the default port 8000.
	// it can be over-ridden through configs
	app.Run()
}

It will lead to the app crash。
But we don’t want other interfaces to be unavailable because of problems with one interface.
The groutine you create yourself should be responsible for its life cycle

@srijan-27
Copy link
Contributor

@Sianao - have you tried running the above code locally?
As I believe it would not crash the app, but instead will return the error stack and app will continue to run.

@Sianao
Copy link
Contributor Author

Sianao commented Jun 17, 2024

@srijan-27
image

this is the test result

@srijan-27
Copy link
Contributor

@Sianao - Thanks! Will check this.

@Sianao
Copy link
Contributor Author

Sianao commented Jun 17, 2024

I will rewrite the test code , because panic(nil) can't be recover on older version with nil check

pkg/gofr/http/errors.go Outdated Show resolved Hide resolved
pkg/gofr/http/errors.go Outdated Show resolved Hide resolved
pkg/gofr/handler.go Outdated Show resolved Hide resolved
@aryanmehrotra aryanmehrotra merged commit 6869de6 into gofr-dev:development Jun 18, 2024
11 checks passed
@aryanmehrotra aryanmehrotra mentioned this pull request Jun 18, 2024
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

Successfully merging this pull request may close these issues.

using file crashes in case of error
3 participants