khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame^] | 1 | package kafka |
| 2 | |
| 3 | /** |
| 4 | * Copyright 2016 Confluent Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | // Automatically generate error codes from librdkafka |
| 20 | // See README for instructions |
| 21 | //go:generate $GOPATH/bin/go_rdkafka_generr generated_errors.go |
| 22 | |
| 23 | /* |
| 24 | #include <librdkafka/rdkafka.h> |
| 25 | */ |
| 26 | import "C" |
| 27 | |
| 28 | // Error provides a Kafka-specific error container |
| 29 | type Error struct { |
| 30 | code ErrorCode |
| 31 | str string |
| 32 | } |
| 33 | |
| 34 | func newError(code C.rd_kafka_resp_err_t) (err Error) { |
| 35 | return Error{ErrorCode(code), ""} |
| 36 | } |
| 37 | |
| 38 | func newGoError(code ErrorCode) (err Error) { |
| 39 | return Error{code, ""} |
| 40 | } |
| 41 | |
| 42 | func newErrorFromString(code ErrorCode, str string) (err Error) { |
| 43 | return Error{code, str} |
| 44 | } |
| 45 | |
| 46 | func newErrorFromCString(code C.rd_kafka_resp_err_t, cstr *C.char) (err Error) { |
| 47 | var str string |
| 48 | if cstr != nil { |
| 49 | str = C.GoString(cstr) |
| 50 | } else { |
| 51 | str = "" |
| 52 | } |
| 53 | return Error{ErrorCode(code), str} |
| 54 | } |
| 55 | |
| 56 | func newCErrorFromString(code C.rd_kafka_resp_err_t, str string) (err Error) { |
| 57 | return newErrorFromString(ErrorCode(code), str) |
| 58 | } |
| 59 | |
| 60 | // Error returns a human readable representation of an Error |
| 61 | // Same as Error.String() |
| 62 | func (e Error) Error() string { |
| 63 | return e.String() |
| 64 | } |
| 65 | |
| 66 | // String returns a human readable representation of an Error |
| 67 | func (e Error) String() string { |
| 68 | if len(e.str) > 0 { |
| 69 | return e.str |
| 70 | } |
| 71 | return e.code.String() |
| 72 | } |
| 73 | |
| 74 | // Code returns the ErrorCode of an Error |
| 75 | func (e Error) Code() ErrorCode { |
| 76 | return e.code |
| 77 | } |