Skip to content

Commit 678a21a

Browse files
committed
return specific schema registry unavailable error.
1 parent 6c34fe4 commit 678a21a

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

avroregistry/errors.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package avroregistry
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// UnavailableError reports an error when the schema registry is unavailable.
8+
type UnavailableError struct {
9+
Cause error
10+
}
11+
12+
// Error implements the error interface.
13+
func (m *UnavailableError) Error() string {
14+
return fmt.Sprintf("schema registry unavailability caused by: %v", m.Cause)
15+
}

avroregistry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (r *Registry) doRequest(req *http.Request, result interface{}) error {
190190
resp, err := http.DefaultClient.Do(req)
191191
if err != nil {
192192
if !attempt.More() || !isTemporaryError(err) {
193-
return err
193+
return &UnavailableError{err}
194194
}
195195
continue
196196
}
@@ -201,7 +201,7 @@ func (r *Registry) doRequest(req *http.Request, result interface{}) error {
201201
if !attempt.More() {
202202
return err
203203
}
204-
if err, ok := err.(*apiError); ok && err.StatusCode/100 != 5 {
204+
if err, ok := err.(*apiError); ok && err.StatusCode != http.StatusInternalServerError {
205205
// It's not a 5xx error. We want to retry on 5xx
206206
// errors, because the Confluent Avro registry
207207
// can occasionally return them as a matter of

avroregistry/registry_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,31 @@ func TestRegister(t *testing.T) {
3636
c.Assert(id1, qt.Equals, id)
3737
}
3838

39+
func TestSchemaRegistryUnavailableError(t *testing.T) {
40+
c := qt.New(t)
41+
ctx := context.Background()
42+
43+
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
44+
45+
}))
46+
47+
// close the server
48+
testServer.Close()
49+
50+
registry, err := avroregistry.New(avroregistry.Params{
51+
ServerURL: testServer.URL,
52+
RetryStrategy: noRetry,
53+
})
54+
c.Assert(err, qt.IsNil)
55+
56+
type R struct {
57+
X int
58+
}
59+
60+
_, err = registry.Register(ctx, randomString(), schemaOf(nil, R{}))
61+
c.Assert(err, qt.ErrorAs, &avroregistry.UnavailableError{})
62+
}
63+
3964
func TestRegisterWithEmptyStruct(t *testing.T) {
4065
c := qt.New(t)
4166

0 commit comments

Comments
 (0)