...

Source file src/edge-infra.dev/pkg/edge/iam/apperror/redirect.go

Documentation: edge-infra.dev/pkg/edge/iam/apperror

     1  package apperror
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  )
     7  
     8  type RedirectError struct {
     9  	code     int
    10  	location string
    11  	message  string
    12  	err      error
    13  }
    14  
    15  // NewRedirectError constructs a RedirectError. HTTP status code is defaulted to 302 (StatusFound)
    16  func NewRedirectError(location string, msg string, err error) *RedirectError {
    17  	return &RedirectError{
    18  		code:     http.StatusFound,
    19  		location: location,
    20  		message:  msg,
    21  		err:      err,
    22  	}
    23  }
    24  
    25  // WithHTTPStatus sets a 3xx HTTP status code for a redirect. Other codes are rejected and will default in 302.
    26  func (e *RedirectError) WithHTTPStatus(code int) *RedirectError {
    27  	e.code = 302
    28  
    29  	if code >= 300 || code < 400 {
    30  		e.code = code
    31  	}
    32  
    33  	return e
    34  }
    35  
    36  func (e RedirectError) Error() string {
    37  	msg := "redirect"
    38  	if e.err != nil {
    39  		msg += fmt.Sprintf(" (%v)", e.err.Error())
    40  	}
    41  	return msg
    42  }
    43  
    44  // Redirect returns the code, location and message
    45  func (e RedirectError) Redirect() (int, string, string) {
    46  	return e.code, e.location, e.message
    47  }
    48  
    49  func (e RedirectError) Unwrap() error {
    50  	return e.err
    51  }
    52  

View as plain text