| 1 |
/* |
| 2 |
Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
or more contributor license agreements. See the NOTICE file |
| 4 |
distributed with this work for additional information |
| 5 |
regarding copyright ownership. The ASF licenses this file |
| 6 |
to you under the Apache License, Version 2.0 (the |
| 7 |
"License"); you may not use this file except in compliance |
| 8 |
with the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, |
| 13 |
software distributed under the License is distributed on an |
| 14 |
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
KIND, either express or implied. See the License for the |
| 16 |
specific language governing permissions and limitations |
| 17 |
under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package amqp |
| 21 |
|
| 22 |
// #include <proton/error.h> |
| 23 |
import "C" |
| 24 |
|
| 25 |
import ( |
| 26 |
"fmt" |
| 27 |
) |
| 28 |
|
| 29 |
// Error is an AMQP error condition. It has a name and a description. |
| 30 |
// It implements the Go error interface so can be returned as an error value. |
| 31 |
// |
| 32 |
// You can pass amqp.Error to methods that send an error to a remote endpoint, |
| 33 |
// this gives you full control over what the remote endpoint will see. |
| 34 |
// |
| 35 |
// You can also pass any Go error to such functions, the remote peer |
| 36 |
// will see the equivalent of MakeError(error) |
| 37 |
// |
| 38 |
type Error struct{ Name, Description string } |
| 39 |
|
| 40 |
// Error implements the Go error interface for AMQP error errors. |
| 41 |
func (c Error) Error() string { return fmt.Sprintf("%s: %s", c.Name, c.Description) } |
| 42 |
|
| 43 |
// Errorf makes a Error with name and formatted description as per fmt.Sprintf |
| 44 |
func Errorf(name, format string, arg ...interface{}) Error { |
| 45 |
return Error{name, fmt.Sprintf(format, arg...)} |
| 46 |
} |
| 47 |
|
| 48 |
// MakeError makes an AMQP error from a go error: {Name: InternalError, Description: err.Error()} |
| 49 |
// If err is already an amqp.Error it is returned unchanged. |
| 50 |
func MakeError(err error) Error { |
| 51 |
if amqpErr, ok := err.(Error); ok { |
| 52 |
return amqpErr |
| 53 |
} else { |
| 54 |
return Error{InternalError, err.Error()} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
var ( |
| 59 |
InternalError = "amqp:internal-error" |
| 60 |
NotFound = "amqp:not-found" |
| 61 |
UnauthorizedAccess = "amqp:unauthorized-access" |
| 62 |
DecodeError = "amqp:decode-error" |
| 63 |
ResourceLimitExceeded = "amqp:resource-limit-exceeded" |
| 64 |
NotAllowed = "amqp:not-allowed" |
| 65 |
InvalidField = "amqp:invalid-field" |
| 66 |
NotImplemented = "amqp:not-implemented" |
| 67 |
ResourceLocked = "amqp:resource-locked" |
| 68 |
PreconditionFailed = "amqp:precondition-failed" |
| 69 |
ResourceDeleted = "amqp:resource-deleted" |
| 70 |
IllegalState = "amqp:illegal-state" |
| 71 |
FrameSizeTooSmall = "amqp:frame-size-too-small" |
| 72 |
) |
| 73 |
|
| 74 |
type PnErrorCode int |
| 75 |
|
| 76 |
func (e PnErrorCode) String() string { |
| 77 |
switch e { |
| 78 |
case C.PN_EOS: |
| 79 |
return "end-of-data" |
| 80 |
case C.PN_ERR: |
| 81 |
return "error" |
| 82 |
case C.PN_OVERFLOW: |
| 83 |
return "overflow" |
| 84 |
case C.PN_UNDERFLOW: |
| 85 |
return "underflow" |
| 86 |
case C.PN_STATE_ERR: |
| 87 |
return "bad-state" |
| 88 |
case C.PN_ARG_ERR: |
| 89 |
return "invalid-argument" |
| 90 |
case C.PN_TIMEOUT: |
| 91 |
return "timeout" |
| 92 |
case C.PN_INTR: |
| 93 |
return "interrupted" |
| 94 |
case C.PN_INPROGRESS: |
| 95 |
return "in-progress" |
| 96 |
default: |
| 97 |
return fmt.Sprintf("unknown-error(%d)", e) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func PnError(e *C.pn_error_t) error { |
| 102 |
if e == nil || C.pn_error_code(e) == 0 { |
| 103 |
return nil |
| 104 |
} |
| 105 |
return fmt.Errorf("%s: %s", PnErrorCode(C.pn_error_code(e)), C.GoString(C.pn_error_text(e))) |
| 106 |
} |