Skip to content

Commit c98e949

Browse files
authored
Merge pull request #3 from fastschema/refactor.update_error_messages_and_examples
refactor: update error messages and examples
2 parents 1e90795 + 3606b15 commit c98e949

8 files changed

Lines changed: 410 additions & 140 deletions

File tree

README.md

Lines changed: 391 additions & 121 deletions
Large diffs are not rendered by default.

common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ func (ac *JsArrayToGoConverter[T]) convertToArray(jsArray *Array, jsLen int64) (
429429
goArrayValue := reflect.New(ac.targetType).Elem()
430430

431431
if jsLen > int64(goArrayLen) {
432-
return ac.sample, fmt.Errorf("DONE_JS array/set length (%d) exceeds Go array length (%d)", jsLen, goArrayLen)
432+
return ac.sample, fmt.Errorf("JS array/set length (%d) exceeds Go array length (%d)", jsLen, goArrayLen)
433433
}
434434

435435
for i := range jsLen {
@@ -474,7 +474,7 @@ func (ac *JsArrayToGoConverter[T]) convertViaJSON(jsArray *Array) (T, error) {
474474

475475
temp := reflect.New(ac.targetType).Interface()
476476
if err = json.Unmarshal([]byte(jsonString), temp); err != nil {
477-
return ac.sample, fmt.Errorf("DONE_can not unmarshal json: %w, input=%s", err, jsonString)
477+
return ac.sample, fmt.Errorf("can not unmarshal json: %w, input=%s", err, jsonString)
478478
}
479479

480480
arrayT, _ := reflect.ValueOf(temp).Elem().Interface().(T)
@@ -563,7 +563,7 @@ func IsTypedArray(input *Value) bool {
563563
// processTempValue validates if temp is a valid result for the given T type.
564564
func processTempValue[T any](prefix string, temp any, err error, samples ...T) (v T, _ error) {
565565
if err != nil {
566-
return v, fmt.Errorf("DONE_[%s] %w", prefix, err)
566+
return v, fmt.Errorf("[%s] %w", prefix, err)
567567
}
568568

569569
// Handle interface{} target types by accepting any converted value
@@ -685,7 +685,7 @@ func StringToNumeric(s string, targetType reflect.Type) (any, error) {
685685
}
686686
}
687687

688-
return nil, fmt.Errorf("DONE_cannot convert JS string %q to %s", s, targetType.String())
688+
return nil, fmt.Errorf("cannot convert JS string %q to %s", s, targetType.String())
689689
}
690690

691691
func createGoObjectTarget[T any](input ObjectOrMap, samples ...T) (

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func newInvalidJsInputErr(kind string, input *Value) (err error) {
111111
detail = fmt.Sprintf("(JSONStringify failed: %v), (.String()) %s", err, input.String())
112112
}
113113

114-
return fmt.Errorf("DONE_expected JS %s, got %s=%s", kind, input.Type(), detail)
114+
return fmt.Errorf("expected JS %s, got %s=%s", kind, input.Type(), detail)
115115
}
116116

117117
func newJsStringifyErr(kind string, err error) error {

functojs.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func FuncToJS(c *Context, v any) (_ *Value, err error) {
1313

1414
defer func() {
1515
if err != nil {
16-
err = fmt.Errorf("DONE_[FuncToJS] %w", err)
16+
err = fmt.Errorf("[FuncToJS] %w", err)
1717
}
1818
}()
1919

@@ -66,11 +66,11 @@ func VerifyGoFunc(fnType reflect.Type) error {
6666
numOut := fnType.NumOut()
6767

6868
if numOut > MaxGoFuncReturnValues {
69-
return fmt.Errorf("DONE_expected 0-2 return values, got '%s'", signature)
69+
return fmt.Errorf("expected 0-2 return values, got '%s'", signature)
7070
}
7171

7272
if numOut == MaxGoFuncReturnValues && !IsImplementError(fnType.Out(1)) {
73-
return fmt.Errorf("DONE_expected second return to be error, got '%s'", signature)
73+
return fmt.Errorf("expected second return to be error, got '%s'", signature)
7474
}
7575

7676
if numOut >= 1 {
@@ -83,7 +83,7 @@ func VerifyGoFunc(fnType reflect.Type) error {
8383
for i := range fnType.NumIn() {
8484
err := IsConvertibleToJs(fnType.In(i), make(map[reflect.Type]bool), "func param")
8585
if err != nil {
86-
return fmt.Errorf("DONE_parameter %d error: %w", i, err)
86+
return fmt.Errorf("parameter %d error: %w", i, err)
8787
}
8888
}
8989

jstogo.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func setFieldWithJSONUnmarshaler(
400400
}
401401

402402
if err := unmarshaler.UnmarshalJSON([]byte(jsonStr)); err != nil {
403-
return fmt.Errorf("DONE_cannot unmarshal json: %w, %s=%s", err, fieldInfo.field.Name, jsonStr)
403+
return fmt.Errorf("cannot unmarshal json: %w, %s=%s", err, fieldInfo.field.Name, jsonStr)
404404
}
405405

406406
fieldValue.Set(unmarshalerValue)
@@ -473,7 +473,7 @@ func jsObjectToGo[T any](
473473
}
474474

475475
if err = json.Unmarshal([]byte(jsonString), tempPtr); err != nil {
476-
err = fmt.Errorf("DONE_can not unmarshal json: %w, input=%s", err, jsonString)
476+
err = fmt.Errorf("can not unmarshal json: %w, input=%s", err, jsonString)
477477

478478
return processTempValue("JsObjectToGo", nil, err, sample)
479479
}
@@ -546,7 +546,7 @@ func createJsFunctionHandler(
546546
jsResult, err := ctx.Invoke(input, ctx.Global(), jsArgs...)
547547
if err != nil {
548548
results[len(results)-1] = reflect.ValueOf(
549-
fmt.Errorf("DONE_JS function execution failed: %w", err),
549+
fmt.Errorf("JS function execution failed: %w", err),
550550
)
551551

552552
return results
@@ -644,7 +644,7 @@ func handleJsFunctionResult(
644644
goResult, err := jsValueToGo[any](tracker, jsResult)
645645
if err != nil {
646646
results[len(results)-1] = reflect.ValueOf(
647-
fmt.Errorf("DONE_failed to convert JS function result '%s' to Go: %w", jsResult.Type(), err),
647+
fmt.Errorf("failed to convert JS function result '%s' to Go: %w", jsResult.Type(), err),
648648
)
649649

650650
return results
@@ -656,7 +656,7 @@ func handleJsFunctionResult(
656656
targetType := fnType.Out(0)
657657
if !goResultValue.CanConvert(targetType) {
658658
results[len(results)-1] = reflect.ValueOf(
659-
fmt.Errorf("DONE_failed to convert JS function return value from %T (%v) to %s", goResult, goResult, targetType),
659+
fmt.Errorf("failed to convert JS function return value from %T (%v) to %s", goResult, goResult, targetType),
660660
)
661661

662662
return results

options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func getRuntimeOption(registry *ProxyRegistry, options ...*Option) (option *Opti
215215

216216
if option.CWD == "" {
217217
if option.CWD, err = os.Getwd(); err != nil {
218-
return nil, fmt.Errorf("DONE_cannot get current working directory: %w", err)
218+
return nil, fmt.Errorf("cannot get current working directory: %w", err)
219219
}
220220
}
221221

testutils_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"github.com/stretchr/testify/require"
1515
)
1616

17-
func setupTestContext(t *testing.T) (*qjs.Runtime, *qjs.Context) {
17+
func setupTestContext(_ *testing.T) (*qjs.Runtime, *qjs.Context) {
1818
runtime := must(qjs.New())
1919
ctx := runtime.Context()
20-
t.Cleanup(runtime.Close)
20+
// t.Cleanup(runtime.Close)
2121
return runtime, ctx
2222
}
2323

value.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func (v *Value) Invoke(fname string, args ...*Value) (*Value, error) {
306306
defer fn.Free()
307307

308308
if !fn.IsFunction() {
309-
return v.NewUndefined(), fmt.Errorf("DONE_JS property '%s' is not a function", fname)
309+
return v.NewUndefined(), fmt.Errorf("JS property '%s' is not a function", fname)
310310
}
311311

312312
argc, argvPtr := createJsCallArgs(v.context, args...)
@@ -560,7 +560,7 @@ func (v *Value) JSONStringify() (_ string, err error) {
560560
defer func() {
561561
r := AnyToError(recover())
562562
if r != nil {
563-
err = fmt.Errorf("DONE_failed to stringify JS value: %w", r)
563+
err = fmt.Errorf("failed to stringify JS value: %w", r)
564564
}
565565
}()
566566

0 commit comments

Comments
 (0)