| 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 |
// Internal implementation details - ignore. |
| 21 |
package proton |
| 22 |
|
| 23 |
// #cgo LDFLAGS: -lqpid-proton-core |
| 24 |
// #include <proton/error.h> |
| 25 |
// #include <proton/codec.h> |
| 26 |
import "C" |
| 27 |
|
| 28 |
import ( |
| 29 |
"fmt" |
| 30 |
"sync" |
| 31 |
"sync/atomic" |
| 32 |
) |
| 33 |
|
| 34 |
type PnErrorCode int |
| 35 |
|
| 36 |
func (e PnErrorCode) String() string { |
| 37 |
switch e { |
| 38 |
case C.PN_EOS: |
| 39 |
return "end-of-data" |
| 40 |
case C.PN_ERR: |
| 41 |
return "error" |
| 42 |
case C.PN_OVERFLOW: |
| 43 |
return "overflow" |
| 44 |
case C.PN_UNDERFLOW: |
| 45 |
return "underflow" |
| 46 |
case C.PN_STATE_ERR: |
| 47 |
return "bad-state" |
| 48 |
case C.PN_ARG_ERR: |
| 49 |
return "invalid-argument" |
| 50 |
case C.PN_TIMEOUT: |
| 51 |
return "timeout" |
| 52 |
case C.PN_INTR: |
| 53 |
return "interrupted" |
| 54 |
case C.PN_INPROGRESS: |
| 55 |
return "in-progress" |
| 56 |
default: |
| 57 |
return fmt.Sprintf("unknown-error(%d)", e) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func PnError(e *C.pn_error_t) error { |
| 62 |
if e == nil || C.pn_error_code(e) == 0 { |
| 63 |
return nil |
| 64 |
} |
| 65 |
return fmt.Errorf("%s: %s", PnErrorCode(C.pn_error_code(e)), C.GoString(C.pn_error_text(e))) |
| 66 |
} |
| 67 |
|
| 68 |
// ErrorHolder is a goroutine-safe error holder that keeps the first error that is set. |
| 69 |
type ErrorHolder struct { |
| 70 |
once sync.Once |
| 71 |
value atomic.Value |
| 72 |
} |
| 73 |
|
| 74 |
// Set the error if not already set |
| 75 |
func (e *ErrorHolder) Set(err error) { |
| 76 |
if err != nil { |
| 77 |
e.once.Do(func() { e.value.Store(err) }) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// Get the error. |
| 82 |
func (e *ErrorHolder) Get() (err error) { |
| 83 |
err, _ = e.value.Load().(error) |
| 84 |
return |
| 85 |
} |