|
Packit |
63bb0d |
package autorest
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Copyright 2017 Microsoft Corporation
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
Packit |
63bb0d |
// you may not use this file except in compliance with the License.
|
|
Packit |
63bb0d |
// You may obtain a copy of the License at
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
63bb0d |
//
|
|
Packit |
63bb0d |
// Unless required by applicable law or agreed to in writing, software
|
|
Packit |
63bb0d |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
63bb0d |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
63bb0d |
// See the License for the specific language governing permissions and
|
|
Packit |
63bb0d |
// limitations under the License.
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
import (
|
|
Packit |
63bb0d |
"fmt"
|
|
Packit |
63bb0d |
"net/http"
|
|
Packit |
63bb0d |
)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
const (
|
|
Packit |
63bb0d |
// UndefinedStatusCode is used when HTTP status code is not available for an error.
|
|
Packit |
63bb0d |
UndefinedStatusCode = 0
|
|
Packit |
63bb0d |
)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// DetailedError encloses a error with details of the package, method, and associated HTTP
|
|
Packit |
63bb0d |
// status code (if any).
|
|
Packit |
63bb0d |
type DetailedError struct {
|
|
Packit |
63bb0d |
Original error
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// PackageType is the package type of the object emitting the error. For types, the value
|
|
Packit |
63bb0d |
// matches that produced the the '%T' format specifier of the fmt package. For other elements,
|
|
Packit |
63bb0d |
// such as functions, it is just the package name (e.g., "autorest").
|
|
Packit |
63bb0d |
PackageType string
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Method is the name of the method raising the error.
|
|
Packit |
63bb0d |
Method string
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// StatusCode is the HTTP Response StatusCode (if non-zero) that led to the error.
|
|
Packit |
63bb0d |
StatusCode interface{}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Message is the error message.
|
|
Packit |
63bb0d |
Message string
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Service Error is the response body of failed API in bytes
|
|
Packit |
63bb0d |
ServiceError []byte
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Response is the response object that was returned during failure if applicable.
|
|
Packit |
63bb0d |
Response *http.Response
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// NewError creates a new Error conforming object from the passed packageType, method, and
|
|
Packit |
63bb0d |
// message. message is treated as a format string to which the optional args apply.
|
|
Packit |
63bb0d |
func NewError(packageType string, method string, message string, args ...interface{}) DetailedError {
|
|
Packit |
63bb0d |
return NewErrorWithError(nil, packageType, method, nil, message, args...)
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// NewErrorWithResponse creates a new Error conforming object from the passed
|
|
Packit |
63bb0d |
// packageType, method, statusCode of the given resp (UndefinedStatusCode if
|
|
Packit |
63bb0d |
// resp is nil), and message. message is treated as a format string to which the
|
|
Packit |
63bb0d |
// optional args apply.
|
|
Packit |
63bb0d |
func NewErrorWithResponse(packageType string, method string, resp *http.Response, message string, args ...interface{}) DetailedError {
|
|
Packit |
63bb0d |
return NewErrorWithError(nil, packageType, method, resp, message, args...)
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// NewErrorWithError creates a new Error conforming object from the
|
|
Packit |
63bb0d |
// passed packageType, method, statusCode of the given resp (UndefinedStatusCode
|
|
Packit |
63bb0d |
// if resp is nil), message, and original error. message is treated as a format
|
|
Packit |
63bb0d |
// string to which the optional args apply.
|
|
Packit |
63bb0d |
func NewErrorWithError(original error, packageType string, method string, resp *http.Response, message string, args ...interface{}) DetailedError {
|
|
Packit |
63bb0d |
if v, ok := original.(DetailedError); ok {
|
|
Packit |
63bb0d |
return v
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
statusCode := UndefinedStatusCode
|
|
Packit |
63bb0d |
if resp != nil {
|
|
Packit |
63bb0d |
statusCode = resp.StatusCode
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
return DetailedError{
|
|
Packit |
63bb0d |
Original: original,
|
|
Packit |
63bb0d |
PackageType: packageType,
|
|
Packit |
63bb0d |
Method: method,
|
|
Packit |
63bb0d |
StatusCode: statusCode,
|
|
Packit |
63bb0d |
Message: fmt.Sprintf(message, args...),
|
|
Packit |
63bb0d |
Response: resp,
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Error returns a formatted containing all available details (i.e., PackageType, Method,
|
|
Packit |
63bb0d |
// StatusCode, Message, and original error (if any)).
|
|
Packit |
63bb0d |
func (e DetailedError) Error() string {
|
|
Packit |
63bb0d |
if e.Original == nil {
|
|
Packit |
63bb0d |
return fmt.Sprintf("%s#%s: %s: StatusCode=%d", e.PackageType, e.Method, e.Message, e.StatusCode)
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
return fmt.Sprintf("%s#%s: %s: StatusCode=%d -- Original Error: %v", e.PackageType, e.Method, e.Message, e.StatusCode, e.Original)
|
|
Packit |
63bb0d |
}
|