Skip to content

Commit

Permalink
Returning the original server error
Browse files Browse the repository at this point in the history
  • Loading branch information
aciduck committed Mar 23, 2020
1 parent 0e77822 commit 16495be
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
25 changes: 13 additions & 12 deletions gremconnect/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func MarshalResponse(msg []byte) (Response, error) {
code = status["code"].(float64)
resp = Response{Code: int(code)}
)
message, _ := status["message"].(string)

err = responseDetectError(resp.Code)
err = responseDetectError(resp.Code, message)
if err != nil {
resp.Data = err // Use the Data field as a vehicle for the error.
} else {
Expand All @@ -67,7 +68,7 @@ func MarshalResponse(msg []byte) (Response, error) {

// responseDetectError detects any possible errors in responses
// from Gremlin Server and generates an error for each code
func responseDetectError(code int) error {
func responseDetectError(code int, message string) error {
switch code {
case 200:
break
Expand All @@ -76,25 +77,25 @@ func responseDetectError(code int) error {
case 206:
break
case 401:
return gremerror.NewNetworkError(401, "UNAUTHORIZED")
return gremerror.NewNetworkError(401, "UNAUTHORIZED", message)
case 407:
return gremerror.NewNetworkError(407, "AUTHENTICATION REQUIRED")
return gremerror.NewNetworkError(407, "AUTHENTICATION REQUIRED", message)
case 498:
return gremerror.NewNetworkError(498, "MALFORMED REQUEST")
return gremerror.NewNetworkError(498, "MALFORMED REQUEST", message)
case 499:
return gremerror.NewNetworkError(499, "INVALID REQUEST ARGUMENTS")
return gremerror.NewNetworkError(499, "INVALID REQUEST ARGUMENTS", message)
case 500:
return gremerror.NewNetworkError(500, "INTERNAL SERVER ERROR")
return gremerror.NewNetworkError(500, "INTERNAL SERVER ERROR", message)
case 503:
return gremerror.NewNetworkError(503, "SERVER UNAVAILABLE")
return gremerror.NewNetworkError(503, "SERVER UNAVAILABLE", message)
case 597:
return gremerror.NewNetworkError(597, "SCRIPT EVALUATION ERROR")
return gremerror.NewNetworkError(597, "SCRIPT EVALUATION ERROR", message)
case 598:
return gremerror.NewNetworkError(598, "SERVER TIMEOUT")
return gremerror.NewNetworkError(598, "SERVER TIMEOUT", message)
case 599:
return gremerror.NewNetworkError(599, "SERIALIZATION ERROR")
return gremerror.NewNetworkError(599, "SERIALIZATION ERROR", message)
default:
return gremerror.NewNetworkError(code, "UNKNOWN ERROR")
return gremerror.NewNetworkError(code, "UNKNOWN ERROR", message)
}
return nil
}
19 changes: 10 additions & 9 deletions gremconnect/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ var (
{
"requestId": "d2476e5b-b2bc-6a70-2647-3991f68ab415",
"status": {
"message": "Some error",
"code": 401,
"attributes": {}
},
Expand Down Expand Up @@ -233,67 +234,67 @@ func TestMarshalRespone(t *testing.T) {
})

Convey("And resp401.Data should be 'UNAUTHORIZED'", func() {
So(resp401.Data, ShouldResemble, gremerror.NewNetworkError(401, "UNAUTHORIZED"))
So(resp401.Data, ShouldResemble, gremerror.NewNetworkError(401, "UNAUTHORIZED", "Some error"))
})

Convey("Then the status code should be 407", func() {
So(resp407.Code, ShouldEqual, 407)
})

Convey("And resp407.Data should be 'AUTHENTICATE'", func() {
So(resp407.Data, ShouldResemble, gremerror.NewNetworkError(407, "AUTHENTICATION REQUIRED"))
So(resp407.Data, ShouldResemble, gremerror.NewNetworkError(407, "AUTHENTICATION REQUIRED", ""))
})

Convey("Then the status code should be 498", func() {
So(resp498.Code, ShouldEqual, 498)
})

Convey("And resp498.Data should be 'MALFORMED REQUEST", func() {
So(resp498.Data, ShouldResemble, gremerror.NewNetworkError(498, "MALFORMED REQUEST"))
So(resp498.Data, ShouldResemble, gremerror.NewNetworkError(498, "MALFORMED REQUEST", ""))
})

Convey("Then the status code should be 499", func() {
So(resp499.Code, ShouldEqual, 499)
})

Convey("And resp499.Data should be 'INVALID REQUEST ARGUMENTS'", func() {
So(resp499.Data, ShouldResemble, gremerror.NewNetworkError(499, "INVALID REQUEST ARGUMENTS"))
So(resp499.Data, ShouldResemble, gremerror.NewNetworkError(499, "INVALID REQUEST ARGUMENTS", ""))
})

Convey("Then the status code should be 500", func() {
So(resp500.Code, ShouldEqual, 500)
})

Convey("And resp500.Data should be 'SERVER ERROR'", func() {
So(resp500.Data, ShouldResemble, gremerror.NewNetworkError(500, "INTERNAL SERVER ERROR"))
So(resp500.Data, ShouldResemble, gremerror.NewNetworkError(500, "INTERNAL SERVER ERROR", ""))
})

Convey("Then the status code should be 597", func() {
So(resp597.Code, ShouldEqual, 597)
})

Convey("And resp597.Data should be 'SCRIPT EVALUATION ERROR'", func() {
So(resp597.Data, ShouldResemble, gremerror.NewNetworkError(597, "SCRIPT EVALUATION ERROR"))
So(resp597.Data, ShouldResemble, gremerror.NewNetworkError(597, "SCRIPT EVALUATION ERROR", ""))
})

Convey("Then the status code should be 598", func() {
So(resp598.Code, ShouldEqual, 598)
})

Convey("And resp598.Data should be 'SERVER TIMEOUT'", func() {
So(resp598.Data, ShouldResemble, gremerror.NewNetworkError(598, "SERVER TIMEOUT"))
So(resp598.Data, ShouldResemble, gremerror.NewNetworkError(598, "SERVER TIMEOUT", ""))
})

Convey("Then the status code should be 599", func() {
So(resp599.Code, ShouldEqual, 599)
})

Convey("And resp599.Data should be 'SERVER SERIALIZATION ERROR'", func() {
So(resp599.Data, ShouldResemble, gremerror.NewNetworkError(599, "SERIALIZATION ERROR"))
So(resp599.Data, ShouldResemble, gremerror.NewNetworkError(599, "SERIALIZATION ERROR", ""))
})

Convey("Then respDefault.Data should be 'UNKNOWN ERROR'", func() {
So(respDefault.Data, ShouldResemble, gremerror.NewNetworkError(403, "UNKNOWN ERROR"))
So(respDefault.Data, ShouldResemble, gremerror.NewNetworkError(403, "UNKNOWN ERROR", ""))
})
})
})
Expand Down
5 changes: 4 additions & 1 deletion gremerror/networkerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import "strconv"
type NetworkError struct {
statusCode int
msg string
origMsg string
}

// NewNetworkError returns a status code related error.
func NewNetworkError(statusCode int, msg string) error {
func NewNetworkError(statusCode int, msg string, origMsg string) error {
return &NetworkError{
statusCode: statusCode,
msg: msg,
origMsg: origMsg,
}
}

Expand All @@ -41,5 +43,6 @@ func (g *NetworkError) Error() string {
fmtError("type", "NETWORK_ERROR"),
fmtError("status code", strconv.Itoa(g.statusCode)),
fmtError("error", g.msg),
fmtError("original error", g.origMsg),
)
}
6 changes: 2 additions & 4 deletions query/graph/makepropertykey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ func TestMakePropertyKey(t *testing.T) {
g := NewGraph()
Convey("When 'MakePropertyKey' is called with a label string, DataType and Cardinality", func() {

var dt datatype.DataType
dt = "String.class"
var cr cardinality.Cardinality
cr = "LIST"
var dt = datatype.String
var cr = cardinality.List
result := g.MakePropertyKey("labelTest", dt, cr)
Convey("Then result should equal 'graph.makePropertyKey('labelTest').dataType(String.class).cardinality(list)'", func() {
So(result.String(), ShouldEqual, "graph.makePropertyKey(\"labelTest\").dataType(String.class).cardinality(list)")
Expand Down

0 comments on commit 16495be

Please sign in to comment.