Skip to content

Commit

Permalink
chore: generalize error msg to error.go (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz authored Mar 22, 2021
1 parent 29e9487 commit fe407d1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
4 changes: 2 additions & 2 deletions api/internal/handler/authentication/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package authentication

import (
"fmt"
"reflect"
"time"

Expand All @@ -29,6 +28,7 @@ import (

"github.com/apisix/manager-api/internal/conf"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/utils/consts"
)

type Handler struct {
Expand Down Expand Up @@ -89,7 +89,7 @@ func (h *Handler) userLogin(c droplet.Context) (interface{}, error) {

user := conf.UserList[username]
if username != user.Username || password != user.Password {
return nil, fmt.Errorf("username or password error")
return nil, consts.ErrUsernamePassword
}

// create JWT for session
Expand Down
4 changes: 2 additions & 2 deletions api/internal/handler/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package consumer

import (
"fmt"
"net/http"
"reflect"
"strings"
Expand All @@ -32,6 +31,7 @@ import (
"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/utils"
"github.com/apisix/manager-api/internal/utils/consts"
)

type Handler struct {
Expand Down Expand Up @@ -139,7 +139,7 @@ func (h *Handler) Set(c droplet.Context) (interface{}, error) {
input := c.Input().(*SetInput)
if input.ID != nil && utils.InterfaceToString(input.ID) != input.Username {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("consumer's id and username must be a same value")
consts.ErrIDUsername
}
if input.Username != "" {
input.Consumer.Username = input.Username
Expand Down
13 changes: 7 additions & 6 deletions api/internal/handler/data_loader/route_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/log"
"github.com/apisix/manager-api/internal/utils"
"github.com/apisix/manager-api/internal/utils/consts"
)

type Handler struct {
Expand Down Expand Up @@ -69,7 +70,7 @@ func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
input := c.Input().(*ExportInput)

if input.IDs == "" {
return nil, fmt.Errorf("Parameter IDs cannot be empty")
return nil, consts.ErrParameterID
}

ids := strings.Split(input.IDs, ",")
Expand All @@ -79,7 +80,7 @@ func (h *Handler) ExportRoutes(c droplet.Context) (interface{}, error) {
route, err := h.routeStore.Get(c.Context(), id)
if err != nil {
if err == data.ErrNotFound {
return nil, fmt.Errorf("route id: %s not found", id)
return nil, fmt.Errorf(consts.IDNotFound, "upstream", id)
}
return nil, err
}
Expand Down Expand Up @@ -113,7 +114,7 @@ func (h *Handler) ExportAllRoutes(c droplet.Context) (interface{}, error) {
routelist, err := h.routeStore.List(c.Context(), store.ListInput{})

if len(routelist.Rows) < 1 {
return nil, fmt.Errorf("Route data is empty, cannot be exported")
return nil, consts.ErrRouteData
}

if err != nil {
Expand Down Expand Up @@ -158,7 +159,7 @@ func (h *Handler) RouteToOpenAPI3(c droplet.Context, routes []*entity.Route) (*o
service, err = h.serviceStore.Get(c.Context(), serviceID)
if err != nil {
if err == data.ErrNotFound {
return nil, fmt.Errorf("service id: %s not found", route.ServiceID)
return nil, fmt.Errorf(consts.IDNotFound, "service", route.ServiceID)
}
return nil, err
}
Expand Down Expand Up @@ -463,7 +464,7 @@ func (h *Handler) ParseRouteUpstream(c droplet.Context, route *entity.Route) (in
upstream, err := h.upstreamStore.Get(c.Context(), upstreamID)
if err != nil {
if err == data.ErrNotFound {
return nil, fmt.Errorf("upstream id: %s not found", route.UpstreamID)
return nil, fmt.Errorf(consts.IDNotFound, "upstream", route.UpstreamID)
}
return nil, err
}
Expand All @@ -477,7 +478,7 @@ func (h *Handler) ParseRouteUpstream(c droplet.Context, route *entity.Route) (in
upstream, err := h.upstreamStore.Get(c.Context(), upstreamID)
if err != nil {
if err == data.ErrNotFound {
return nil, fmt.Errorf("upstream id: %s not found", _service.UpstreamID)
return nil, fmt.Errorf(consts.IDNotFound, "upstream", _service.UpstreamID)
}
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions api/internal/handler/data_loader/route_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"path"
Expand Down Expand Up @@ -95,7 +94,7 @@ func (h *ImportHandler) Import(c droplet.Context) (interface{}, error) {

if len(swagger.Paths) < 1 {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
errors.New("empty or invalid imported file")
consts.ErrImportFile
}

routes, err := OpenAPI3ToRoute(swagger)
Expand All @@ -116,7 +115,7 @@ func (h *ImportHandler) Import(c droplet.Context) (interface{}, error) {
if err != nil {
if err == data.ErrNotFound {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("service id: %s not found", route.ServiceID)
fmt.Errorf(consts.IDNotFound, "service", route.ServiceID)
}
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err
}
Expand All @@ -126,7 +125,7 @@ func (h *ImportHandler) Import(c droplet.Context) (interface{}, error) {
if err != nil {
if err == data.ErrNotFound {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("upstream id: %s not found", route.UpstreamID)
fmt.Errorf(consts.IDNotFound, "upstream", route.UpstreamID)
}
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err
}
Expand Down
4 changes: 2 additions & 2 deletions api/internal/handler/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (h *Handler) Create(c droplet.Context) (interface{}, error) {
if err != nil {
if err == data.ErrNotFound {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("service id: %s not found", input.ServiceID)
fmt.Errorf(consts.IDNotFound, "service", input.ServiceID)
}
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func (h *Handler) Update(c droplet.Context) (interface{}, error) {
if err != nil {
if err == data.ErrNotFound {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("service id: %s not found", input.ServiceID)
fmt.Errorf(consts.IDNotFound, "service", input.ServiceID)
}
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest}, err
}
Expand Down
9 changes: 4 additions & 5 deletions api/internal/handler/ssl/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -330,23 +329,23 @@ func (h *Handler) BatchDelete(c droplet.Context) (interface{}, error) {

func ParseCert(crt, key string) (*entity.SSL, error) {
if crt == "" || key == "" {
return nil, errors.New("invalid certificate")
return nil, consts.ErrSSLCertificate
}

certDERBlock, _ := pem.Decode([]byte(crt))
if certDERBlock == nil {
return nil, errors.New("Certificate resolution failed")
return nil, consts.ErrSSLCertificateResolution
}
// match
_, err := tls.X509KeyPair([]byte(crt), []byte(key))
if err != nil {
return nil, errors.New("key and cert don't match")
return nil, consts.ErrSSLKeyAndCert
}

x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes)

if err != nil {
return nil, errors.New("Certificate resolution failed")
return nil, consts.ErrSSLCertificateResolution
}

ssl := entity.SSL{}
Expand Down
30 changes: 29 additions & 1 deletion api/internal/utils/consts/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,41 @@
*/
package consts

import "github.com/shiningrush/droplet/data"
import (
"errors"

"github.com/shiningrush/droplet/data"
)

const (
ErrBadRequest = 20001
ErrForbidden = 20002
)

const (
// IDNotFound is the string use for can't find the cache by the id
IDNotFound = "%s id: %s not found"
)

var (
// ErrorUsernamePassword is the error means username or password is not correct
ErrUsernamePassword = errors.New("username or password error")
// ErrorIDUsername is the error use for the input's id and username is different
ErrIDUsername = errors.New("consumer's id and username must be a same value")
// ErrorParameterID is the error use for parameter ID is empty
ErrParameterID = errors.New("Parameter IDs cannot be empty")
// ErrorRouteData is the error that the route data is empty
ErrRouteData = errors.New("Route data is empty, cannot be exported")
// ErrorImportFile is the error that use for import a empty file
ErrImportFile = errors.New("empty or invalid imported file")
// ErrorImportFile means the certificate is invalid
ErrSSLCertificate = errors.New("invalid certificate")
// ErrorSSLCertificateResolution means the SSL certificate decode failed
ErrSSLCertificateResolution = errors.New("Certificate resolution failed")
// ErrorSSLKeyAndCert means the SSL key and SSL certificate don't match
ErrSSLKeyAndCert = errors.New("key and cert don't match")
)

var (
// base error please refer to github.com/shiningrush/droplet/data, such as data.ErrNotFound, data.ErrConflicted
ErrInvalidRequest = data.BaseError{Code: ErrBadRequest, Message: "invalid request"}
Expand Down

0 comments on commit fe407d1

Please sign in to comment.