subrouters
This commit is contained in:
@@ -67,12 +67,12 @@ func (b bodyRes) GetStatus() (int, bool) {
|
||||
return b.res.GetStatus()
|
||||
}
|
||||
|
||||
func (b bodyRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
func (b bodyRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
||||
if b.res == nil {
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
return b.res.getRedirect()
|
||||
return b.res.GetRedirect()
|
||||
}
|
||||
|
||||
func (b bodyRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
||||
|
||||
@@ -66,12 +66,12 @@ func (c cookieRes) GetStatus() (int, bool) {
|
||||
return c.res.GetStatus()
|
||||
}
|
||||
|
||||
func (c cookieRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
func (c cookieRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
||||
if c.res == nil {
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
return c.res.getRedirect()
|
||||
return c.res.GetRedirect()
|
||||
}
|
||||
|
||||
func (c cookieRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"ruben/inventory2/internal/consts"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -133,3 +134,27 @@ func GetError(err error) (e ErrorResponse, ok bool) {
|
||||
ok = errors.As(err, &e)
|
||||
return e, ok
|
||||
}
|
||||
|
||||
// error wrapping utilities
|
||||
|
||||
func ErrorFromConstant(err error) error {
|
||||
cerr := err
|
||||
for cerr != nil {
|
||||
switch cerr {
|
||||
case consts.ErrNotFound:
|
||||
return NotFound()
|
||||
case consts.ErrConflict:
|
||||
return Conflict()
|
||||
}
|
||||
|
||||
uerr, ok := cerr.(interface {
|
||||
Unwrap() error
|
||||
})
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
||||
cerr = uerr.Unwrap()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -68,12 +68,12 @@ func (h htmlRes) GetStatus() (int, bool) {
|
||||
return h.res.GetStatus()
|
||||
}
|
||||
|
||||
func (h htmlRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
func (h htmlRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
||||
if h.res == nil {
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
return h.res.getRedirect()
|
||||
return h.res.GetRedirect()
|
||||
}
|
||||
|
||||
func (h htmlRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
||||
|
||||
@@ -69,12 +69,12 @@ func (j jsonRes) GetStatus() (int, bool) {
|
||||
return j.res.GetStatus()
|
||||
}
|
||||
|
||||
func (j jsonRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
func (j jsonRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
||||
if j.res == nil {
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
return j.res.getRedirect()
|
||||
return j.res.GetRedirect()
|
||||
}
|
||||
|
||||
func (j jsonRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
||||
|
||||
@@ -107,7 +107,7 @@ func (r redirectRes) GetStatus() (int, bool) {
|
||||
return r.res.GetStatus()
|
||||
}
|
||||
|
||||
func (r redirectRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
func (r redirectRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
||||
return r.code, r.to, true
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ type (
|
||||
|
||||
GetStatus() (code int, ok bool)
|
||||
getBody() (body io.ReadCloser, ok bool, err error)
|
||||
getRedirect() (code redirect.Code, to string, ok bool)
|
||||
GetRedirect() (code redirect.Code, to string, ok bool)
|
||||
getCookies() []http.Cookie
|
||||
getContentType() (contentType string, ok bool)
|
||||
|
||||
|
||||
@@ -64,12 +64,12 @@ func (s statusRes) GetStatus() (int, bool) {
|
||||
return s.code, true
|
||||
}
|
||||
|
||||
func (s statusRes) getRedirect() (code redirect.Code, to string, ok bool) {
|
||||
func (s statusRes) GetRedirect() (code redirect.Code, to string, ok bool) {
|
||||
if s.res == nil {
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
return s.res.getRedirect()
|
||||
return s.res.GetRedirect()
|
||||
}
|
||||
|
||||
func (s statusRes) getBody() (body io.ReadCloser, ok bool, err error) {
|
||||
|
||||
@@ -18,7 +18,7 @@ func Write(w http.ResponseWriter, r *http.Request, res Response) {
|
||||
hdrs.Add("Content-Type", ct)
|
||||
}
|
||||
|
||||
if code, to, ok := res.getRedirect(); ok {
|
||||
if code, to, ok := res.GetRedirect(); ok {
|
||||
http.Redirect(w, r, to, code.Int())
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user