storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/api-error-response.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2016 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package madmin
    18  
    19  import (
    20  	"encoding/xml"
    21  	"fmt"
    22  	"net/http"
    23  )
    24  
    25  /* **** SAMPLE ERROR RESPONSE ****
    26  <?xml version="1.0" encoding="UTF-8"?>
    27  <Error>
    28     <Code>AccessDenied</Code>
    29     <Message>Access Denied</Message>
    30     <BucketName>bucketName</BucketName>
    31     <Key>objectName</Key>
    32     <RequestId>F19772218238A85A</RequestId>
    33     <HostId>GuWkjyviSiGHizehqpmsD1ndz5NClSP19DOT+s2mv7gXGQ8/X1lhbDGiIJEXpGFD</HostId>
    34  </Error>
    35  */
    36  
    37  // ErrorResponse - Is the typed error returned by all API operations.
    38  type ErrorResponse struct {
    39  	XMLName    xml.Name `xml:"Error" json:"-"`
    40  	Code       string
    41  	Message    string
    42  	BucketName string
    43  	Key        string
    44  	RequestID  string `xml:"RequestId"`
    45  	HostID     string `xml:"HostId"`
    46  
    47  	// Region where the bucket is located. This header is returned
    48  	// only in HEAD bucket and ListObjects response.
    49  	Region string
    50  }
    51  
    52  // Error - Returns HTTP error string
    53  func (e ErrorResponse) Error() string {
    54  	return e.Message
    55  }
    56  
    57  const (
    58  	reportIssue = "Please report this issue at https://github.com/minio/minio/issues."
    59  )
    60  
    61  // httpRespToErrorResponse returns a new encoded ErrorResponse
    62  // structure as error.
    63  func httpRespToErrorResponse(resp *http.Response) error {
    64  	if resp == nil {
    65  		msg := "Response is empty. " + reportIssue
    66  		return ErrInvalidArgument(msg)
    67  	}
    68  	var errResp ErrorResponse
    69  	// Decode the json error
    70  	err := jsonDecoder(resp.Body, &errResp)
    71  	if err != nil {
    72  		return ErrorResponse{
    73  			Code:    resp.Status,
    74  			Message: fmt.Sprintf("Failed to parse server response: %s.", err),
    75  		}
    76  	}
    77  	closeResponse(resp)
    78  	return errResp
    79  }
    80  
    81  // ToErrorResponse - Returns parsed ErrorResponse struct from body and
    82  // http headers.
    83  //
    84  // For example:
    85  //
    86  //   import admin "github.com/minio/minio/pkg/madmin"
    87  //   ...
    88  //   ...
    89  //   ss, err := adm.ServiceStatus(...)
    90  //   if err != nil {
    91  //      resp := admin.ToErrorResponse(err)
    92  //   }
    93  //   ...
    94  func ToErrorResponse(err error) ErrorResponse {
    95  	switch err := err.(type) {
    96  	case ErrorResponse:
    97  		return err
    98  	default:
    99  		return ErrorResponse{}
   100  	}
   101  }
   102  
   103  // ErrInvalidArgument - Invalid argument response.
   104  func ErrInvalidArgument(message string) error {
   105  	return ErrorResponse{
   106  		Code:      "InvalidArgument",
   107  		Message:   message,
   108  		RequestID: "minio",
   109  	}
   110  }