github.com/go-kivik/kivik/v4@v4.3.2/pouchdb/bindings/errors_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 //go:build js 14 15 package bindings 16 17 import ( 18 "testing" 19 20 "github.com/gopherjs/gopherjs/js" 21 22 _ "github.com/go-kivik/kivik/v4/pouchdb/bindings/poucherr" 23 ) 24 25 type statuser interface { // nolint:misspell 26 HTTPStatus() int 27 } 28 29 func TestNewPouchError(t *testing.T) { 30 type npeTest struct { 31 Name string 32 Object *js.Object 33 ExpectedStatus int 34 Expected string 35 } 36 tests := []npeTest{ 37 { 38 Name: "Null", 39 Object: nil, 40 Expected: "", 41 }, 42 { 43 Name: "NameAndReasonNoStatus", 44 Object: func() *js.Object { 45 o := js.Global.Get("Object").New() 46 o.Set("reason", "error reason") 47 o.Set("name", "error name") 48 return o 49 }(), 50 ExpectedStatus: 500, 51 Expected: "error name: error reason", 52 }, 53 { 54 Name: "ECONNREFUSED", 55 Object: js.Global.Call("ReconstitutePouchError", `{ 56 "code": "ECONNREFUSED", 57 "errno": "ECONNREFUSED", 58 "syscall": "connect", 59 "address": "127.0.0.1", 60 "port": 5984, 61 "status": 500, 62 "result": { 63 "ok": false, 64 "start_time": "Tue May 16 2017 08:26:31 GMT+0000 (UTC)", 65 "docs_read": 0, 66 "docs_written": 0, 67 "doc_write_failures": 0, 68 "errors": [], 69 "status": "aborting", 70 "end_time": "Tue May 16 2017 08:26:31 GMT+0000 (UTC)", 71 "last_seq": 0 72 } 73 }`), 74 ExpectedStatus: 500, 75 Expected: "Error: connection refused", 76 }, 77 } 78 for _, test := range tests { 79 func(test npeTest) { 80 t.Run(test.Name, func(t *testing.T) { 81 result := NewPouchError(test.Object) 82 var msg string 83 if result != nil { 84 msg = result.Error() 85 } 86 if msg != test.Expected { 87 t.Errorf("Expected error: %s\n Actual error: %s", test.Expected, msg) 88 } 89 if result == nil { 90 return 91 } 92 status := result.(statuser).HTTPStatus() // nolint:misspell 93 if status != test.ExpectedStatus { 94 t.Errorf("Expected status %d, got %d", test.ExpectedStatus, status) 95 } 96 }) 97 }(test) 98 } 99 }