Skip to content

Commit fa73ec2

Browse files
committed
Addressing review comments
1 parent 30840f2 commit fa73ec2

8 files changed

+16
-22
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/couchbase/sg-bucket
33
go 1.19
44

55
require (
6-
github.com/pkg/errors v0.9.1
76
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
87
github.com/snej/v8go v1.7.3
98
github.com/stretchr/testify v1.7.1
@@ -16,6 +15,7 @@ require (
1615
github.com/golang/snappy v0.0.4 // indirect
1716
github.com/google/uuid v1.3.0 // indirect
1817
github.com/opentracing/opentracing-go v1.2.0 // indirect
18+
github.com/pkg/errors v0.9.1 // indirect
1919
github.com/pmezard/go-difflib v1.0.0 // indirect
2020
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 // indirect
2121
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect

js/otto_runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ package js
1313
import (
1414
"context"
1515
"encoding/json"
16+
"errors"
1617
"fmt"
1718
"strconv"
1819

19-
"github.com/pkg/errors"
2020
"github.com/robertkrimen/otto"
2121

2222
_ "github.com/robertkrimen/otto/underscore"

js/otto_vm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ func (vm *ottoVM) getRunner(service *Service) (Runner, error) {
6868
return nil, fmt.Errorf("the js.VM has been closed")
6969
}
7070
if vm.curRunner != nil {
71-
panic("illegal access to v8VM: already has a v8Runner")
71+
panic("illegal access to ottoVM: already has a ottoRunner")
7272
}
7373
if !vm.services.hasService(service) {
74-
return nil, fmt.Errorf("unknown js.Service instance passed to VM")
74+
return nil, fmt.Errorf("unknown js.Service instance passed to VM: %v", service)
7575
}
7676
if service.v8Init != nil {
7777
return nil, fmt.Errorf("js.Service has custom initialization not supported by Otto")

js/v8_runner.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ package js
1515
import (
1616
"context"
1717
"encoding/json"
18+
"errors"
1819
"fmt"
1920
"strings"
2021
"time"
2122

22-
"github.com/pkg/errors"
2323
v8 "github.com/snej/v8go" // Docs: https://pkg.go.dev/github.com/snej/v8go
2424
)
2525

@@ -32,22 +32,25 @@ type V8Runner struct {
3232
Client any // You can put whatever you want here, to point back to your state
3333
}
3434

35-
func newV8Runner(vm *v8VM, template V8Template, id serviceID) (*V8Runner, error) {
35+
func newV8Runner(vm *v8VM, template V8Template, id serviceID) (runner *V8Runner, err error) {
3636
// Create a V8 Context and run the setup script in it:
3737
ctx := v8.NewContext(vm.iso, template.Global())
38+
defer func() {
39+
if err != nil {
40+
ctx.Close()
41+
}
42+
}()
3843
if _, err := vm.setupScript.Run(ctx); err != nil {
39-
return nil, errors.Wrap(err, "Unexpected error in JavaScript initialization code")
44+
return nil, fmt.Errorf("Unexpected error in JavaScript initialization code: %w", err)
4045
}
4146

4247
// Now run the service's script, which returns the service's main function:
4348
result, err := template.Script().Run(ctx)
4449
if err != nil {
45-
ctx.Close()
4650
return nil, fmt.Errorf("JavaScript error initializing %s: %w", template.Name(), err)
4751
}
4852
mainFn, err := result.AsFunction()
4953
if err != nil {
50-
ctx.Close()
5154
return nil, fmt.Errorf("%s's script did not return a function: %w", template.Name(), err)
5255
}
5356

js/v8_template.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package js
1515
import (
1616
"fmt"
1717

18-
"github.com/pkg/errors"
1918
v8 "github.com/snej/v8go"
2019
)
2120

@@ -114,7 +113,7 @@ func (t *V8BasicTemplate) NewCallback(callback TemplateCallback) *v8.FunctionTem
114113
if v8Result, newValErr := runner.NewValue(result); err == nil {
115114
return v8Result
116115
} else {
117-
err = errors.Wrap(newValErr, "Could not convert a callback's result to JavaScript")
116+
err = fmt.Errorf("Could not convert a callback's result to JavaScript: %w", newValErr)
118117
}
119118
}
120119
return v8Throw(vm.iso, err)

js/v8_utils.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@ func newJSONString(ctx *v8.Context, val any) (*v8.Value, error) {
114114
// Returns an error back to a V8 caller.
115115
// Calls v8.Isolate.ThrowException, with the Go error's string as the message.
116116
func v8Throw(i *v8.Isolate, err error) *v8.Value {
117-
var errStr string
118-
// if httpErr, ok := err.(*base.HTTPError); ok {
119-
// errStr = fmt.Sprintf("[%d] %s", httpErr.Status, httpErr.Message)
120-
// } else {
121-
errStr = err.Error()
122-
// }
123-
return i.ThrowException(newString(i, errStr))
117+
return i.ThrowException(newString(i, err.Error()))
124118
}
125119

126120
// Simple utility to wrap a function that returns a value and an error; returns just the value, panicking if there was an error.

js/v8_vm.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"sync"
1919
"time"
2020

21-
"github.com/pkg/errors"
2221
v8 "github.com/snej/v8go"
2322
)
2423

@@ -123,7 +122,7 @@ func (vm *v8VM) release() {
123122
func (vm *v8VM) getTemplate(service *Service) (V8Template, error) {
124123
var tmpl V8Template
125124
if !vm.services.hasService(service) {
126-
return nil, fmt.Errorf("unknown js.Service instance passed to VM")
125+
return nil, fmt.Errorf("unknown js.Service instance passed to VM: %v", service)
127126
}
128127
if int(service.id) < len(vm.templates) {
129128
tmpl = vm.templates[service.id]
@@ -134,7 +133,7 @@ func (vm *v8VM) getTemplate(service *Service) (V8Template, error) {
134133
var err error
135134
vm.setupScript, err = vm.iso.CompileUnboundScript(kSetupLoggingJS+kUnderscoreJS, "setupScript.js", v8.CompileOptions{})
136135
if err != nil {
137-
return nil, errors.Wrapf(err, "Couldn't compile setup script")
136+
return nil, fmt.Errorf("Couldn't compile setup script: %w", err)
138137
}
139138
}
140139

js/vmpool_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ func TestPoolsSequentially(t *testing.T) {
6868

6969
func TestPoolsConcurrently(t *testing.T) {
7070
maxProcs := runtime.GOMAXPROCS(0)
71-
log.Printf("FYI, GOMAXPROCS = %d", maxProcs)
7271
if !assert.GreaterOrEqual(t, maxProcs, 2, "Not enough OS threads available") {
7372
return
7473
}

0 commit comments

Comments
 (0)