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

Allow customizable behavior on cors failure #30

Open
ablankz opened this issue May 3, 2024 · 0 comments
Open

Allow customizable behavior on cors failure #30

ablankz opened this issue May 3, 2024 · 0 comments

Comments

@ablankz
Copy link
Contributor

ablankz commented May 3, 2024

When CORS fails, the current process always takes the same behavior as shown below, which is not applicable to a wide range of use cases.

Therefore, it would be easier to use if the following functions could be optionally set.

// ErrorHandler is a custom error handler function for handling CORS errors.
// if you want to write success response or call next handler, return true.
// If you want to terminate further processing, return false.
ErrorHandler func(w http.ResponseWriter, r *http.Request, cors Cors, err error) bool

We also suggest that you define your own error structure to identify the errors passed here.

// Error is an interface for CORS errors.
type Error interface {
	CorsError()
}

// PreflightError is an interface for preflight errors.
type PreflightError interface {
	PreflightCorsError()
}

// ActualRequestError is an interface for actual request errors.
type ActualRequestError interface {
	ActualRequestCorsError()
}

// PreflightNotOptionMethodError is returned when the method is not allowed.
type PreflightNotOptionMethodError struct {
	Method string
}

func (e *PreflightNotOptionMethodError) Error() string {
	return fmt.Sprintf("Preflight aborted: %s!=OPTIONS", e.Method)
}

// Is implements the Is method of the error interface.
func (e *PreflightNotOptionMethodError) Is(target error) bool {
	_, ok := target.(*PreflightNotOptionMethodError)
	return ok
}

// As implements the As method of the error interface.
func (e *PreflightNotOptionMethodError) As(target any) bool {
	switch target.(type) {
	case **PreflightNotOptionMethodError:
		return true
	default:
		return false
	}
}

// PreflightCorsError implements the PreflightCorsError interface.
func (e *PreflightNotOptionMethodError) PreflightCorsError() {}

// CorsError implements the CorsError interface.
func (e *PreflightNotOptionMethodError) CorsError() {}

The above code is just an example, and we believe that the structure should be separated for each error that is currently possible as a failure.

ablankz added a commit to ablankz/cors that referenced this issue May 3, 2024
ablankz added a commit to ablankz/cors that referenced this issue May 3, 2024
ablankz added a commit to ablankz/cors that referenced this issue May 3, 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

No branches or pull requests

1 participant