Skip to content

Commit 65a064e

Browse files
authored
Merge pull request #65 from go-kivik/errors-v3
Remove use of kivik/errors package
2 parents d36bad0 + b690f0f commit 65a064e

File tree

7 files changed

+23
-21
lines changed

7 files changed

+23
-21
lines changed

bindings/errors.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/gopherjs/gopherjs/js"
88
"github.com/gopherjs/jsbuiltin"
99

10-
"github.com/go-kivik/kivik/v3/errors"
10+
"github.com/go-kivik/kivik/v3"
1111
)
1212

1313
type pouchError struct {
@@ -35,7 +35,7 @@ func NewPouchError(o *js.Object) error {
3535
msg = o.Get("message").String()
3636
default:
3737
if jsbuiltin.InstanceOf(o, js.Global.Get("Error")) {
38-
return errors.Status(status, o.Get("message").String())
38+
return &kivik.Error{HTTPStatus: status, Message: o.Get("message").String()}
3939
}
4040
}
4141
switch {

bindings/pouchdb.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"bytes"
77
"context"
88
"encoding/json"
9+
"errors"
910
"io"
1011
"net/http"
1112
"time"
1213

1314
"github.com/gopherjs/gopherjs/js"
1415
"github.com/gopherjs/jsbuiltin"
1516

16-
"github.com/go-kivik/kivik/v3/errors"
17+
"github.com/go-kivik/kivik/v3"
1718
)
1819

1920
// DB is a PouchDB database object.
@@ -189,7 +190,7 @@ func (db *DB) Query(ctx context.Context, ddoc, view string, options map[string]i
189190
return callBack(ctx, db, "query", ddoc+"/"+view, o)
190191
}
191192

192-
var findPluginNotLoaded = errors.Status(http.StatusNotImplemented, "kivik: pouchdb-find plugin not loaded")
193+
var findPluginNotLoaded = &kivik.Error{HTTPStatus: http.StatusNotImplemented, Message: "kivik: pouchdb-find plugin not loaded"}
193194

194195
// Find executes a MongoDB-style find query with the pouchdb-find plugin, if it
195196
// is installed. If the plugin is not installed, a NotImplemented error will be
@@ -223,7 +224,10 @@ func Objectify(i interface{}) (interface{}, error) {
223224
}
224225
var x interface{}
225226
err := json.Unmarshal(buf, &x)
226-
return x, errors.WrapStatus(http.StatusBadRequest, err)
227+
if err != nil {
228+
err = &kivik.Error{HTTPStatus: http.StatusBadRequest, Err: err}
229+
}
230+
return x, err
227231
}
228232

229233
// Compact compacts the database, and waits for it to complete. This may take

bulk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
"github.com/gopherjs/gopherjs/js"
99

10+
kivik "github.com/go-kivik/kivik/v3"
1011
"github.com/go-kivik/kivik/v3/driver"
11-
"github.com/go-kivik/kivik/v3/errors"
1212
)
1313

1414
type bulkResult struct {
@@ -47,7 +47,7 @@ func (r *bulkResults) Next(update *driver.BulkResult) (err error) {
4747
update.Rev = result.ID
4848
update.Error = nil
4949
if result.IsError {
50-
update.Error = errors.Status(result.StatusCode, result.Reason)
50+
update.Error = &kivik.Error{HTTPStatus: result.StatusCode, Message: result.Reason}
5151
}
5252
return nil
5353
}

db.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212

1313
kivik "github.com/go-kivik/kivik/v3"
1414
"github.com/go-kivik/kivik/v3/driver"
15-
"github.com/go-kivik/kivik/v3/errors"
1615
"github.com/go-kivik/pouchdb/v3/bindings"
1716
)
1817

@@ -79,7 +78,7 @@ func (d *db) Put(ctx context.Context, docID string, doc interface{}, options map
7978
jsDoc := js.Global.Get("JSON").Call("parse", string(jsonDoc))
8079
if id := jsDoc.Get("_id"); id != js.Undefined {
8180
if id.String() != docID {
82-
return "", errors.Status(http.StatusBadRequest, "id argument must match _id field in document")
81+
return "", &kivik.Error{HTTPStatus: http.StatusBadRequest, Message: "id argument must match _id field in document"}
8382
}
8483
}
8584
jsDoc.Set("_id", docID)
@@ -102,7 +101,7 @@ func (d *db) Stats(ctx context.Context) (*driver.DBStats, error) {
102101

103102
func (d *db) Compact(_ context.Context) error {
104103
if atomic.LoadUint32(&d.compacting) == 1 {
105-
return &kivik.Error{HTTPStatus: http.StatusTooManyRequests, Err: errors.New("kivik: compaction already running")}
104+
return &kivik.Error{HTTPStatus: http.StatusTooManyRequests, Message: "kivik: compaction already running"}
106105
}
107106
atomic.StoreUint32(&d.compacting, 1)
108107
defer atomic.StoreUint32(&d.compacting, 0)
@@ -116,14 +115,14 @@ func (d *db) CompactView(_ context.Context, _ string) error {
116115

117116
func (d *db) ViewCleanup(_ context.Context) error {
118117
if atomic.LoadUint32(&d.viewCleanup) == 1 {
119-
return &kivik.Error{HTTPStatus: http.StatusTooManyRequests, Err: errors.New("kivik: view cleanup already running")}
118+
return &kivik.Error{HTTPStatus: http.StatusTooManyRequests, Message: "kivik: view cleanup already running"}
120119
}
121120
atomic.StoreUint32(&d.viewCleanup, 1)
122121
defer atomic.StoreUint32(&d.viewCleanup, 0)
123122
return d.db.ViewCleanup()
124123
}
125124

126-
var securityNotImplemented = errors.Status(http.StatusNotImplemented, "kivik: security interface not supported by PouchDB")
125+
var securityNotImplemented = &kivik.Error{HTTPStatus: http.StatusNotImplemented, Message: "kivik: security interface not supported by PouchDB"}
127126

128127
func (d *db) Security(ctx context.Context) (*driver.Security, error) {
129128
return nil, securityNotImplemented

find.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
"github.com/gopherjs/gopherjs/js"
1111

12+
kivik "github.com/go-kivik/kivik/v3"
1213
"github.com/go-kivik/kivik/v3/driver"
13-
"github.com/go-kivik/kivik/v3/errors"
1414
"github.com/go-kivik/pouchdb/v3/bindings"
1515
)
1616

@@ -76,7 +76,7 @@ func (d *db) findIndex(ctx context.Context, ddoc, name string) (interface{}, err
7676
}, nil
7777
}
7878
}
79-
return nil, errors.Status(http.StatusNotFound, "index does not exist")
79+
return nil, &kivik.Error{HTTPStatus: http.StatusNotFound, Message: "index does not exist"}
8080
}
8181

8282
func (d *db) DeleteIndex(ctx context.Context, ddoc, name string) error {

pouchdb.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pouchdb
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"net/http"
89
"net/url"
@@ -11,7 +12,6 @@ import (
1112

1213
kivik "github.com/go-kivik/kivik/v3"
1314
"github.com/go-kivik/kivik/v3/driver"
14-
"github.com/go-kivik/kivik/v3/errors"
1515
"github.com/go-kivik/pouchdb/v3/bindings"
1616
)
1717

@@ -137,7 +137,7 @@ func (c *client) DBExists(ctx context.Context, dbName string, options map[string
137137
func (c *client) CreateDB(ctx context.Context, dbName string, options map[string]interface{}) error {
138138
if c.isRemote() {
139139
if exists, _ := c.DBExists(ctx, dbName, options); exists {
140-
return errors.Status(http.StatusPreconditionFailed, "database exists")
140+
return &kivik.Error{HTTPStatus: http.StatusPreconditionFailed, Message: "database exists"}
141141
}
142142
}
143143
opts := c.options(options)
@@ -153,7 +153,7 @@ func (c *client) DestroyDB(ctx context.Context, dbName string, options map[strin
153153
}
154154
if !exists {
155155
// This will only ever do anything for a remote database
156-
return errors.Status(http.StatusNotFound, "database does not exist")
156+
return &kivik.Error{HTTPStatus: http.StatusNotFound, Message: "database does not exist"}
157157
}
158158
return c.pouch.New(c.dbURL(dbName), opts).Destroy(ctx, nil)
159159
}
@@ -165,7 +165,7 @@ func (c *client) DB(ctx context.Context, dbName string, options map[string]inter
165165
return nil, err
166166
}
167167
if !exists {
168-
return nil, errors.Status(http.StatusNotFound, "database does not exist")
168+
return nil, &kivik.Error{HTTPStatus: http.StatusNotFound, Message: "database does not exist"}
169169
}
170170
return &db{
171171
db: c.pouch.New(c.dbURL(dbName), opts),

replication.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
kivik "github.com/go-kivik/kivik/v3"
1212
"github.com/go-kivik/kivik/v3/driver"
13-
"github.com/go-kivik/kivik/v3/errors"
1413
"github.com/go-kivik/pouchdb/v3/bindings"
1514
)
1615

@@ -105,7 +104,7 @@ func (r *replication) Delete(ctx context.Context) (err error) {
105104
return nil
106105
}
107106
}
108-
return errors.Status(http.StatusNotFound, "replication not found")
107+
return &kivik.Error{HTTPStatus: http.StatusNotFound, Message: "replication not found"}
109108
}
110109

111110
func replicationEndpoint(dsn string, object interface{}) (name string, obj interface{}, err error) {
@@ -148,7 +147,7 @@ func (c *client) Replicate(_ context.Context, targetDSN, sourceDSN string, optio
148147

149148
func (c *client) GetReplications(_ context.Context, options map[string]interface{}) ([]driver.Replication, error) {
150149
for range options {
151-
return nil, errors.Status(http.StatusBadRequest, "options not yet supported")
150+
return nil, &kivik.Error{HTTPStatus: http.StatusBadRequest, Message: "options not yet supported"}
152151
}
153152
c.replicationsMU.RLock()
154153
defer c.replicationsMU.RUnlock()

0 commit comments

Comments
 (0)