github.com/avenga/couper@v1.12.2/errors/couper.go (about)

     1  package errors
     2  
     3  import (
     4  	"net/http"
     5  	"reflect"
     6  	"strings"
     7  	"unicode"
     8  )
     9  
    10  const Wildcard = "*"
    11  
    12  var (
    13  	AccessControl    = &Error{synopsis: "access control error", kinds: []string{"access_control"}, httpStatus: http.StatusForbidden}
    14  	Backend          = &Error{synopsis: "backend error", Contexts: []string{"api", "endpoint"}, kinds: []string{"backend"}, httpStatus: http.StatusBadGateway}
    15  	ClientRequest    = &Error{synopsis: "client request error", httpStatus: http.StatusBadRequest}
    16  	Endpoint         = &Error{synopsis: "endpoint error", Contexts: []string{"endpoint"}, kinds: []string{"endpoint"}, httpStatus: http.StatusBadGateway}
    17  	Evaluation       = &Error{synopsis: "expression evaluation error", kinds: []string{"evaluation"}, httpStatus: http.StatusInternalServerError}
    18  	Configuration    = &Error{synopsis: "configuration error", kinds: []string{"configuration"}, httpStatus: http.StatusInternalServerError}
    19  	MethodNotAllowed = &Error{synopsis: "method not allowed error", httpStatus: http.StatusMethodNotAllowed}
    20  	Proxy            = &Error{synopsis: "proxy error", httpStatus: http.StatusBadGateway}
    21  	Request          = &Error{synopsis: "request error", httpStatus: http.StatusBadGateway}
    22  	RouteNotFound    = &Error{synopsis: "route not found error", httpStatus: http.StatusNotFound}
    23  	Server           = &Error{synopsis: "internal server error", httpStatus: http.StatusInternalServerError}
    24  	ServerShutdown   = &Error{synopsis: "server shutdown error", httpStatus: http.StatusInternalServerError}
    25  )
    26  
    27  func TypeToSnake(t interface{}) string {
    28  	typeStr := reflect.TypeOf(t).String()
    29  	if strings.Contains(typeStr, ".") { // package name removal
    30  		typeStr = strings.Split(typeStr, ".")[1]
    31  	}
    32  	var result []rune
    33  	var previous rune
    34  	for i, r := range typeStr {
    35  		if i > 0 && unicode.IsUpper(r) && unicode.IsLower(previous) {
    36  			result = append(result, '_')
    37  		}
    38  		result = append(result, unicode.ToLower(r))
    39  		previous = r
    40  	}
    41  
    42  	return string(result)
    43  }
    44  
    45  func SnakeToCamel(str string) string {
    46  	var result []rune
    47  	if len(str) == 0 {
    48  		return str
    49  	}
    50  	result = append(result, unicode.ToUpper(rune(str[0])))
    51  	var upperNext bool
    52  	for _, r := range str[1:] {
    53  		if r == '_' {
    54  			upperNext = true
    55  			continue
    56  		}
    57  		if upperNext {
    58  			result = append(result, unicode.ToUpper(r))
    59  		} else {
    60  			result = append(result, unicode.ToLower(r))
    61  		}
    62  		upperNext = false
    63  	}
    64  	return string(result)
    65  }