Просмотр исходного кода

add error code for unavailable due to ce

Alexander Belanger 4 лет назад
Родитель
Сommit
e93f471cd6
4 измененных файлов с 20 добавлено и 2 удалено
  1. 1 1
      .air.toml
  2. 3 1
      api/server/handlers/handler.go
  3. 9 0
      api/server/shared/apierrors/errors.go
  4. 7 0
      api/types/error.go

+ 1 - 1
.air.toml

@@ -7,7 +7,7 @@ tmp_dir = "tmp"
 
 [build]
 # Just plain old shell command. You could use `make` as well.
-cmd = "go build -o ./tmp/app -tags ee ./cmd/app"
+cmd = "go build -o ./tmp/app ./cmd/app"
 # Binary file yields from `cmd`.
 bin = "tmp/app"
 # Customize binary.

+ 3 - 1
api/server/handlers/handler.go

@@ -122,5 +122,7 @@ func (u *Unavailable) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	apierrors.HandleAPIError(u.config, w, r, apierrors.NewErrPassThroughToClient(
 		fmt.Errorf("%s not available in community edition", u.handlerID),
 		http.StatusBadRequest,
-	), true)
+	), true, apierrors.ErrorOpts{
+		Code: types.ErrCodeUnavailable,
+	})
 }

+ 9 - 0
api/server/shared/apierrors/errors.go

@@ -90,12 +90,17 @@ func (e *ErrPassThroughToClient) GetStatusCode() int {
 	return e.statusCode
 }
 
+type ErrorOpts struct {
+	Code uint
+}
+
 func HandleAPIError(
 	config *config.Config,
 	w http.ResponseWriter,
 	r *http.Request,
 	err RequestError,
 	writeErr bool,
+	opts ...ErrorOpts,
 ) {
 	extErrorStr := err.ExternalError()
 
@@ -123,6 +128,10 @@ func HandleAPIError(
 			Error: extErrorStr,
 		}
 
+		if len(opts) > 0 {
+			resp.Code = opts[0].Code
+		}
+
 		// write the status code
 		w.WriteHeader(err.GetStatusCode())
 

+ 7 - 0
api/types/error.go

@@ -1,5 +1,12 @@
 package types
 
+const (
+	ErrCodeUnavailable uint = 601
+)
+
 type ExternalError struct {
+	// Optional error code for well-known error types
+	Code uint `json:"code,omitempty"`
+
 	Error string `json:"error"`
 }