Skip to content

Commit

Permalink
Merge pull request #6 from spiegel-im-spiegel/fix-bugs-and-testing
Browse files Browse the repository at this point in the history
Bump up version of errs package
  • Loading branch information
spiegel-im-spiegel authored Aug 12, 2020
2 parents 8d9c978 + 5f25281 commit f4814af
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 197 deletions.
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ builds:
- freebsd
goarch:
- amd64
- "386"
- arm
- arm64
main: ./
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: go

go:
- "1.13.x"
- "1.15.x"

env:
global:
Expand Down
17 changes: 8 additions & 9 deletions api/aozoraapi/aozoraapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,35 @@ func (a *AozoraAPI) Name() string {
return a.svcType.String()
}

///LookupRawData returns openBD raw data
//LookupRawData returns openBD raw data
func (a *AozoraAPI) LookupRawData(id string) (io.Reader, error) {
bookId, err := strconv.Atoi(id)
if err != nil {
return nil, errs.Wrap(
err,
return nil, errs.New(
fmt.Sprintf("invalid book id: %v", id),
errs.WithContext("id", id),
)
}
b, err := a.server.CreateClient(aozora.WithContext(a.ctx), aozora.WithHttpClient(&http.Client{})).LookupBookRaw(bookId)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}
return bytes.NewReader(b), nil
}

///LookupBook returns Book data from openBD
//LookupBook returns Book data from openBD
func (a *AozoraAPI) LookupBook(id string) (*entity.Book, error) {
bookId, err := strconv.Atoi(id)
if err != nil {
return nil, errs.Wrap(
err,
return nil, errs.New(
fmt.Sprintf("invalid book id: %v", id),
errs.WithCause(err),
errs.WithContext("id", id),
)
}
bk, err := a.server.CreateClient(aozora.WithContext(a.ctx), aozora.WithHttpClient(&http.Client{})).LookupBook(bookId)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}

book := &entity.Book{
Expand Down Expand Up @@ -104,7 +103,7 @@ func getCreators(bk *aozora.Book) []entity.Creator {
return creators
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
16 changes: 8 additions & 8 deletions api/openbd/openbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (a *OpenBD) Name() string {
func (a *OpenBD) LookupRawData(id string) (io.Reader, error) {
res, err := a.server.CreateClient(obd.WithContext(a.ctx), obd.WithHttpClient(&http.Client{})).LookupBooksRaw([]string{id})
if err != nil {
return nil, errs.Wrap(
err,
return nil, errs.New(
fmt.Sprintf("invalid book id: %v", id),
errs.WithCause(err),
errs.WithContext("id", id),
)
}
Expand All @@ -52,14 +52,14 @@ func (a *OpenBD) LookupRawData(id string) (io.Reader, error) {
func (a *OpenBD) LookupBook(id string) (*entity.Book, error) {
data, err := a.LookupRawData(id)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}
bd, err := unmarshalJSON(data)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}
if !bd.IsValid() {
return nil, errs.Wrap(ecode.ErrInvalidAPIResponse, "", errs.WithContext("id", id))
return nil, errs.Wrap(ecode.ErrInvalidAPIResponse, errs.WithContext("id", id))
}

book := &entity.Book{
Expand Down Expand Up @@ -96,15 +96,15 @@ func getCreators(bd *obd.Book) []entity.Creator {
func unmarshalJSON(jsondata io.Reader) (*obd.Book, error) {
books := []obd.Book{}
if err := json.NewDecoder(jsondata).Decode(&books); err != nil {
return nil, errs.Wrap(err, "")
return nil, errs.Wrap(err)
}
if len(books) == 0 {
return nil, errs.Wrap(ecode.ErrNoData, "")
return nil, errs.Wrap(ecode.ErrNoData)
}
return &books[0], nil
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
10 changes: 5 additions & 5 deletions api/pa/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ func (r *Response) CheckError() error {

func (r *Response) Output(typeName string) (*entity.Book, error) {
if r == nil {
return nil, errs.Wrap(ecode.ErrNullPointer, "")
return nil, errs.Wrap(ecode.ErrNullPointer)
}
if err := r.CheckError(); err != nil {
return nil, errs.Wrap(err, "")
return nil, errs.Wrap(err)
}
if len(r.ItemsResult.Items) == 0 {
return nil, errs.Wrap(ecode.ErrNoData, "")
return nil, errs.Wrap(ecode.ErrNoData)
}
item := r.ItemsResult.Items[0]
book := &entity.Book{
Expand All @@ -243,7 +243,7 @@ func (r *Response) Output(typeName string) (*entity.Book, error) {
//JSON returns JSON data from Response instance
func (r *Response) JSON() ([]byte, error) {
b, err := json.Marshal(r)
return b, errs.Wrap(err, "")
return b, errs.Wrap(err)
}

//Stringer
Expand All @@ -255,7 +255,7 @@ func (r *Response) String() string {
return string(b)
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
10 changes: 5 additions & 5 deletions api/pa/paapi5.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (a *PAAPI5) LookupRawData(id string) (io.Reader, error) {
q := NewQuery(client.Marketplace(), client.PartnerTag(), client.PartnerType(), []string{id})
body, err := client.Request(q)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}

return bytes.NewReader(body), nil
Expand All @@ -64,17 +64,17 @@ func (a *PAAPI5) LookupRawData(id string) (io.Reader, error) {
func (a *PAAPI5) LookupBook(id string) (*entity.Book, error) {
r, err := a.LookupRawData(id)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}
rsp := Response{}
if err := json.NewDecoder(r).Decode(&rsp); err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("id", id))
return nil, errs.Wrap(err, errs.WithContext("id", id))
}
book, err := rsp.Output(a.Name())
return book, errs.Wrap(err, "", errs.WithContext("id", id))
return book, errs.Wrap(err, errs.WithContext("id", id))
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
6 changes: 3 additions & 3 deletions api/pa/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (q *Query) Operation() paapi5.Operation {

func (q *Query) Payload() ([]byte, error) {
if q == nil {
return nil, errs.Wrap(paapi5.ErrNullPointer, "")
return nil, errs.Wrap(paapi5.ErrNullPointer)
}
q.Resources = []string{
"Images.Primary.Medium",
Expand All @@ -54,7 +54,7 @@ func (q *Query) Payload() ([]byte, error) {
"ItemInfo.Title",
}
b, err := json.Marshal(q)
return b, errs.Wrap(err, "")
return b, errs.Wrap(err)
}

func (q *Query) String() string {
Expand All @@ -65,7 +65,7 @@ func (q *Query) String() string {
return string(b)
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
93 changes: 1 addition & 92 deletions ecode/ecode_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package ecode

import (
"errors"
"fmt"
"testing"

"github.com/spiegel-im-spiegel/errs"
)

func TestECodeError(t *testing.T) {
Expand All @@ -32,95 +29,7 @@ func TestECodeError(t *testing.T) {
}
}

func TestECodeErrorEquality(t *testing.T) {
testCases := []struct {
err1 error
err2 error
res bool
}{
{err1: ErrNullPointer, err2: ErrNullPointer, res: true},
{err1: ErrNullPointer, err2: errs.Wrap(ErrNullPointer, "wrapping error"), res: false},
{err1: ErrNullPointer, err2: nil, res: false},
{err1: ErrNullPointer, err2: ECode(0), res: false},
}

for _, tc := range testCases {
res := errors.Is(tc.err1, tc.err2)
if res != tc.res {
t.Errorf("\"%v\" == \"%v\" ? %v, want %v", tc.err1, tc.err2, res, tc.res)
}
}
}

func TestWrapError(t *testing.T) {
testCases := []struct {
err error
msg string
str string
}{
{err: ErrNullPointer, msg: "wrapping error", str: "wrapping error: Null reference instance"},
}

for _, tc := range testCases {
we := errs.Wrap(tc.err, tc.msg)
if we.Error() != tc.str {
t.Errorf("wrapError.Error() == \"%v\", want \"%v\"", we.Error(), tc.str)
}
fmt.Printf("Info(TestWrapError): %+v\n", we)
}
}

func TestWrapNilError(t *testing.T) {
if we := errs.Wrap(nil, "null error"); we != nil {
t.Errorf("Wrap(nil) == \"%v\", want nil.", we)
}
}

func TestWrapfError(t *testing.T) {
testCases := []struct {
err error
msg string
str string
}{
{err: ErrNullPointer, msg: "wrapping error", str: "wrapping error: Null reference instance"},
}

for _, tc := range testCases {
we := errs.Wrap(tc.err, tc.msg)
if we.Error() != tc.str {
t.Errorf("wrapError.Error() == \"%v\", want \"%v\"", we.Error(), tc.str)
}
fmt.Printf("Info(TestWrapfError): %+v\n", we)
}
}

func TestWrapfNilError(t *testing.T) {
if we := errs.Wrap(nil, "null error"); we != nil {
t.Errorf("Wrapf(nil) == \"%v\", want nil.", we)
}
}

func TestWrapErrorEquality(t *testing.T) {
testCases := []struct {
err1 error
err2 error
res bool
}{
{err1: errs.Wrap(ErrNullPointer, "wrapping error"), err2: ErrNullPointer, res: true},
{err1: errs.Wrap(ErrNullPointer, "wrapping error"), err2: nil, res: false},
{err1: errs.Wrap(ErrNullPointer, "wrapping error"), err2: ECode(0), res: false},
{err1: errs.Wrap(ErrNullPointer, "wrapping error"), err2: errs.Wrap(ECode(0), "wrapping error"), res: false},
}

for _, tc := range testCases {
res := errors.Is(tc.err1, tc.err2)
if res != tc.res {
t.Errorf("\"%v\" == \"%v\" ? %v, want %v", tc.err1, tc.err2, res, tc.res)
}
}
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
10 changes: 5 additions & 5 deletions entity/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,23 @@ func ImportBookFromJSON(r io.Reader) (*Book, error) {
dec := json.NewDecoder(r)
bk := Book{}
err := dec.Decode(&bk)
return &bk, errs.Wrap(err, "")
return &bk, errs.Wrap(err)
}

func (b *Book) Format(tmpltPath string) ([]byte, error) {
if b == nil {
return nil, errs.Wrap(ecode.ErrNullPointer, "", errs.WithContext("tmpltPath", tmpltPath))
return nil, errs.Wrap(ecode.ErrNullPointer, errs.WithContext("tmpltPath", tmpltPath))
}
if len(tmpltPath) == 0 {
b, err := json.Marshal(b)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("tmpltPath", tmpltPath))
return nil, errs.Wrap(err, errs.WithContext("tmpltPath", tmpltPath))
}
return b, nil
}
buf, err := format.ByTemplateFile(b, tmpltPath)
if err != nil {
return nil, errs.Wrap(err, "", errs.WithContext("tmpltPath", tmpltPath))
return nil, errs.Wrap(err, errs.WithContext("tmpltPath", tmpltPath))
}
return buf.Bytes(), nil
}
Expand All @@ -117,7 +117,7 @@ func (b *Book) CopyFrom(src *Book) *Book {
return b
}

/* Copyright 2019 Spiegel
/* Copyright 2019,2020 Spiegel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions entity/values/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (t *Date) UnmarshalJSON(b []byte) error {
var lastErr error
for _, tmplt := range timeTemplate1 {
if tm, err := time.Parse(tmplt, s); err != nil {
lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
lastErr = errs.Wrap(err, errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
} else {
*t = Date{tm}
return nil
Expand All @@ -57,7 +57,7 @@ func (t *Date) UnmarshalJSON(b []byte) error {
var lastErr error
for _, tmplt := range timeTemplate2 {
if tm, err := time.Parse(tmplt, s); err != nil {
lastErr = errs.Wrap(err, "", errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
lastErr = errs.Wrap(err, errs.WithContext("time_string", s), errs.WithContext("time_template", tmplt))
} else {
*t = Date{tm}
return nil
Expand Down
Loading

0 comments on commit f4814af

Please sign in to comment.