| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package proto |
| 15 |
|
| 16 |
import ( |
| 17 |
"encoding/json" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
|
| 21 |
"github.com/notaryproject/notation-plugin-framework-go/plugin" |
| 22 |
) |
| 23 |
|
| 24 |
// Deprecated: ErrorCode exists for historical compatibility and should not be used. |
| 25 |
// To access ErrorCode, use the notation-plugin-framework-go's plugin.ErrorCode type. |
| 26 |
type ErrorCode = plugin.ErrorCode |
| 27 |
|
| 28 |
const ( |
| 29 |
// ErrorCodeValidation is used when any of the required request fields is empty ormalformed/invalid. |
| 30 |
// |
| 31 |
// Deprecated: ErrorCodeValidation exists for historical compatibility and should not be used. |
| 32 |
// To access ErrorCodeValidation, use the notation-plugin-framework-go's [plugin.ErrorCodeValidation]. |
| 33 |
ErrorCodeValidation = plugin.ErrorCodeValidation |
| 34 |
|
| 35 |
// ErrorCodeUnsupportedContractVersion is used when when the contract version used in the request is unsupported. |
| 36 |
// |
| 37 |
// Deprecated: ErrorCodeUnsupportedContractVersion exists for historical compatibility and should not be used. |
| 38 |
// To access ErrorCodeUnsupportedContractVersion, use the notation-plugin-framework-go's [plugin.ErrorCodeUnsupportedContractVersion]. |
| 39 |
ErrorCodeUnsupportedContractVersion = plugin.ErrorCodeUnsupportedContractVersion |
| 40 |
|
| 41 |
// ErrorCodeAccessDenied is used when user doesn't have required permission to access the key. |
| 42 |
// |
| 43 |
// Deprecated: ErrorCodeAccessDenied exists for historical compatibility and should not be used. |
| 44 |
// To access ErrorCodeAccessDenied, use the notation-plugin-framework-go's [plugin.ErrorCodeAccessDenied]. |
| 45 |
ErrorCodeAccessDenied = plugin.ErrorCodeAccessDenied |
| 46 |
|
| 47 |
// ErrorCodeTimeout is used when an operation to generate signature timed out and can be retried by Notation. |
| 48 |
// |
| 49 |
// Deprecated: ErrorCodeTimeout exists for historical compatibility and should not be used. |
| 50 |
// To access ErrorCodeTimeout, use the notation-plugin-framework-go's [plugin.ErrorCodeTimeout]. |
| 51 |
ErrorCodeTimeout = plugin.ErrorCodeTimeout |
| 52 |
|
| 53 |
// ErrorCodeThrottled is used when an operation to generate signature was throttles |
| 54 |
// and can be retried by Notation. |
| 55 |
// |
| 56 |
// Deprecated: ErrorCodeThrottled exists for historical compatibility and should not be used. |
| 57 |
// To access ErrorCodeThrottled, use the notation-plugin-framework-go's [plugin.ErrorCodeThrottled]. |
| 58 |
ErrorCodeThrottled = plugin.ErrorCodeThrottled |
| 59 |
|
| 60 |
// ErrorCodeGeneric is used when an general error occurred that does not fall into any categories. |
| 61 |
// |
| 62 |
// Deprecated: ErrorCodeGeneric exists for historical compatibility and should not be used. |
| 63 |
// To access ErrorCodeGeneric, use the notation-plugin-framework-go's [plugin.ErrorCodeGeneric]. |
| 64 |
ErrorCodeGeneric = plugin.ErrorCodeGeneric |
| 65 |
) |
| 66 |
|
| 67 |
// RequestError is the common error response for any request. |
| 68 |
type RequestError struct { |
| 69 |
Code plugin.ErrorCode |
| 70 |
Err error |
| 71 |
Metadata map[string]string |
| 72 |
} |
| 73 |
|
| 74 |
func (e RequestError) Error() string { |
| 75 |
return fmt.Sprintf("%v", e.Err) |
| 76 |
} |
| 77 |
|
| 78 |
func (e RequestError) Unwrap() error { |
| 79 |
return e.Err |
| 80 |
} |
| 81 |
|
| 82 |
func (e RequestError) Is(target error) bool { |
| 83 |
if et, ok := target.(RequestError); ok { |
| 84 |
if e.Code != et.Code { |
| 85 |
return false |
| 86 |
} |
| 87 |
if e.Err == et.Err { |
| 88 |
return true |
| 89 |
} |
| 90 |
return e.Err != nil && et.Err != nil && e.Err.Error() == et.Err.Error() |
| 91 |
} |
| 92 |
return false |
| 93 |
} |
| 94 |
|
| 95 |
func (e RequestError) MarshalJSON() ([]byte, error) { |
| 96 |
var msg string |
| 97 |
if e.Err != nil { |
| 98 |
msg = e.Err.Error() |
| 99 |
} |
| 100 |
return json.Marshal(plugin.Error{ErrCode: e.Code, Message: msg, Metadata: e.Metadata}) |
| 101 |
} |
| 102 |
|
| 103 |
func (e *RequestError) UnmarshalJSON(data []byte) error { |
| 104 |
var tmp plugin.Error |
| 105 |
err := json.Unmarshal(data, &tmp) |
| 106 |
if err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
if tmp.ErrCode == "" && tmp.Message == "" && tmp.Metadata == nil { |
| 110 |
return errors.New("incomplete json") |
| 111 |
} |
| 112 |
*e = RequestError{Code: tmp.ErrCode, Metadata: tmp.Metadata} |
| 113 |
if tmp.Message != "" { |
| 114 |
e.Err = errors.New(tmp.Message) |
| 115 |
} |
| 116 |
return nil |
| 117 |
} |