Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Tika http status code in errors returned by client methods #25

Merged
merged 6 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion tika/tika.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ import (
"golang.org/x/net/context/ctxhttp"
)

// ClientError is returned by Client's various parse methods and
// represents an error response from the Tika server. Example usage:
//
// client := tika.NewClient(nil, tikaURL)
// s, err := client.Parse(context.Background(), input)
// var tikaErr tika.ClientError
// if errors.As(err, &tikaErr) {
// switch tikaErr.StatusCode {
// case http.StatusUnsupportedMediaType, http.StatusUnprocessableEntity:
// // Handle content related error
// default:
// // Handle possibly intermittent http error
// }
// } else if err != nil {
// // Handle non-http error
// }
type ClientError struct {
// StatusCode is the HTTP status code returned by the Tika server.
StatusCode int
}

func (e ClientError) Error() string {
return fmt.Sprintf("response code %d", e.StatusCode)
}

// Client represents a connection to a Tika Server.
tomyl marked this conversation as resolved.
Show resolved Hide resolved
type Client struct {
// url is the URL of the Tika Server, including the port (if necessary), but
Expand Down Expand Up @@ -107,7 +132,7 @@ func (c *Client) call(ctx context.Context, input io.Reader, method, path string,
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("response code %v", resp.StatusCode)
return nil, ClientError{resp.StatusCode}
}
return ioutil.ReadAll(resp.Body)
}
Expand Down
28 changes: 24 additions & 4 deletions tika/tika_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tika
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -75,8 +76,9 @@ func TestParse(t *testing.T) {

func TestParseRecursive(t *testing.T) {
tests := []struct {
response string
want []string
response string
want []string
statusCode int
}{
{
response: `[{"X-TIKA:content":"test 1"}]`,
Expand All @@ -93,16 +95,34 @@ func TestParseRecursive(t *testing.T) {
{
response: `[]`,
},
{
statusCode: http.StatusUnprocessableEntity,
},
}
for _, test := range tests {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprint(w, test.response)
if test.statusCode != 0 {
w.WriteHeader(test.statusCode)
} else {
fmt.Fprint(w, test.response)
}
}))
defer ts.Close()
c := NewClient(nil, ts.URL)
got, err := c.ParseRecursive(context.Background(), nil)
if err != nil {
t.Errorf("ParseRecursive returned an error: %v, want %v", err, test.want)
if test.statusCode != 0 {
var tikaErr ClientError
if errors.As(err, &tikaErr) {
if tikaErr.StatusCode != test.statusCode {
t.Errorf("ParseRecursive expected status code %d, got %d", test.statusCode, tikaErr.StatusCode)
}
} else {
t.Errorf("ParseRecursive expected TikaError, got %T", err)
}
} else {
t.Errorf("ParseRecursive returned an error: %v, want %v", err, test.want)
}
continue
}
if !reflect.DeepEqual(got, test.want) {
Expand Down