github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/openapi/common.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package openapi
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	"net/http"
    20  	"strings"
    21  
    22  	"github.com/pingcap/log"
    23  	"github.com/pingcap/tiflow/pkg/errors"
    24  	"go.uber.org/zap"
    25  )
    26  
    27  // JobAPIPrefix is the prefix of the job API.
    28  const JobAPIPrefix = "/api/v1/jobs/"
    29  
    30  // JobDetailAPIFormat is the path format for job detail status
    31  // the entire path is: /api/v1/jobs/${jobID}/status
    32  const JobDetailAPIFormat = JobAPIPrefix + "%s/status"
    33  
    34  // HTTPError is the error format for http response.
    35  type HTTPError struct {
    36  	Code    string `json:"code"`
    37  	Message string `json:"message"`
    38  }
    39  
    40  // NewHTTPError creates a new HTTPError.
    41  func NewHTTPError(err error) *HTTPError {
    42  	rfcCode, ok := errors.RFCCode(err)
    43  	if !ok {
    44  		rfcCode = errors.ErrUnknown.RFCCode()
    45  	}
    46  	return &HTTPError{
    47  		Code:    string(rfcCode),
    48  		Message: strings.TrimPrefix(err.Error(), fmt.Sprintf("[%s]", rfcCode)),
    49  	}
    50  }
    51  
    52  // WriteHTTPError writes error to http response with normalized error format.
    53  func WriteHTTPError(w http.ResponseWriter, err error) {
    54  	httpErr := NewHTTPError(err)
    55  	w.Header().Set("Content-Type", "application/json")
    56  	w.WriteHeader(errors.HTTPStatusCode(err))
    57  	if err := json.NewEncoder(w).Encode(httpErr); err != nil {
    58  		log.Warn("Failed to write error response", zap.Error(err))
    59  	}
    60  }