Skip to content

Commit

Permalink
chore: improve error messages (#482)
Browse files Browse the repository at this point in the history
Improve error messages so we don't just get TypeError with no clue what
the issue was.
  • Loading branch information
stevenh committed Dec 7, 2022
1 parent 7927fb4 commit ddcbf14
Show file tree
Hide file tree
Showing 26 changed files with 173 additions and 143 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- name: Validate go mod / generate
run: |
go mod tidy
go install golang.org/x/tools/cmd/stringer@latest
go generate ./...
git --no-pager diff && [[ 0 -eq $(git status --porcelain | wc -l) ]]
Expand Down
24 changes: 12 additions & 12 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestArray_toLocaleString(t *testing.T) {

test(`raise:
[ { toLocaleString: undefined } ].toLocaleString();
`, "TypeError")
`, `TypeError: Array.toLocaleString index[0] "undefined" is not callable`)
})
}

Expand Down Expand Up @@ -429,9 +429,9 @@ func TestArray_every(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].every()`, "TypeError")
test(`raise: [].every()`, `TypeError: Array.every argument "undefined" is not callable`)

test(`raise: [].every("abc")`, "TypeError")
test(`raise: [].every("abc")`, `TypeError: Array.every argument "abc" is not callable`)

test(`[].every(function() { return false })`, true)

Expand Down Expand Up @@ -479,7 +479,7 @@ func TestArray_some(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].some("abc")`, "TypeError")
test(`raise: [].some("abc")`, `TypeError: Array.some "abc" if not callable`)

test(`[].some(function() { return true })`, false)

Expand Down Expand Up @@ -521,7 +521,7 @@ func TestArray_forEach(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].forEach("abc")`, "TypeError")
test(`raise: [].forEach("abc")`, `TypeError: Array.foreach "abc" if not callable`)

test(`
var abc = 0;
Expand Down Expand Up @@ -594,7 +594,7 @@ func TestArray_map(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].map("abc")`, "TypeError")
test(`raise: [].map("abc")`, `TypeError: Array.foreach "abc" if not callable`)

test(`[].map(function() { return 1 }).length`, 0)

Expand Down Expand Up @@ -626,7 +626,7 @@ func TestArray_filter(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].filter("abc")`, "TypeError")
test(`raise: [].filter("abc")`, `TypeError: Array.filter "abc" if not callable`)

test(`[].filter(function() { return 1 }).length`, 0)

Expand All @@ -640,9 +640,9 @@ func TestArray_reduce(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].reduce("abc")`, "TypeError")
test(`raise: [].reduce("abc")`, `TypeError: Array.reduce "abc" if not callable`)

test(`raise: [].reduce(function() {})`, "TypeError")
test(`raise: [].reduce(function() {})`, `TypeError: Array.reduce "function() {}" if not callable`)

test(`[].reduce(function() {}, 0)`, 0)

Expand All @@ -660,9 +660,9 @@ func TestArray_reduceRight(t *testing.T) {
tt(t, func() {
test, _ := test()

test(`raise: [].reduceRight("abc")`, "TypeError")
test(`raise: [].reduceRight("abc")`, `TypeError: Array.reduceRight "abc" if not callable`)

test(`raise: [].reduceRight(function() {})`, "TypeError")
test(`raise: [].reduceRight(function() {})`, `TypeError: Array.reduceRight "function() {}" if not callable`)

test(`[].reduceRight(function() {}, 0)`, 0)

Expand Down Expand Up @@ -697,7 +697,7 @@ func TestArray_defineOwnProperty(t *testing.T) {
Object.defineProperty(abc, "length", {
writable: true
});
`, "TypeError")
`, `TypeError: Object.DefineOwnProperty: property not configurable or writeable and descriptor not writeable`)
})
}

Expand Down
18 changes: 9 additions & 9 deletions builtin_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func builtinArrayToLocaleString(call FunctionCall) Value {
obj := call.runtime.toObject(value)
toLocaleString := obj.get("toLocaleString")
if !toLocaleString.isCallable() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.toLocaleString index[%d] %q is not callable", index, toLocaleString))
}
stringValue = toLocaleString.call(call.runtime, objectValue(obj)).string()
}
Expand Down Expand Up @@ -457,7 +457,7 @@ func builtinArraySort(call FunctionCall) Value {
compare := compareValue.object()
if compareValue.IsUndefined() {
} else if !compareValue.isCallable() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.sort value %q is not callable", compareValue))
}
if length > 1 {
arraySortQuickSort(thisObject, 0, length-1, compare)
Expand Down Expand Up @@ -541,7 +541,7 @@ func builtinArrayEvery(call FunctionCall) Value {
}
return trueValue
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.every argument %q is not callable", call.Argument(0)))
}

func builtinArraySome(call FunctionCall) Value {
Expand All @@ -559,7 +559,7 @@ func builtinArraySome(call FunctionCall) Value {
}
return falseValue
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.some %q if not callable", call.Argument(0)))
}

func builtinArrayForEach(call FunctionCall) Value {
Expand All @@ -575,7 +575,7 @@ func builtinArrayForEach(call FunctionCall) Value {
}
return Value{}
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.foreach %q if not callable", call.Argument(0)))
}

func builtinArrayMap(call FunctionCall) Value {
Expand All @@ -594,7 +594,7 @@ func builtinArrayMap(call FunctionCall) Value {
}
return objectValue(call.runtime.newArrayOf(values))
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.foreach %q if not callable", call.Argument(0)))
}

func builtinArrayFilter(call FunctionCall) Value {
Expand All @@ -614,7 +614,7 @@ func builtinArrayFilter(call FunctionCall) Value {
}
return objectValue(call.runtime.newArrayOf(values))
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.filter %q if not callable", call.Argument(0)))
}

func builtinArrayReduce(call FunctionCall) Value {
Expand Down Expand Up @@ -647,7 +647,7 @@ func builtinArrayReduce(call FunctionCall) Value {
return accumulator
}
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.reduce %q if not callable", call.Argument(0)))
}

func builtinArrayReduceRight(call FunctionCall) Value {
Expand Down Expand Up @@ -679,5 +679,5 @@ func builtinArrayReduceRight(call FunctionCall) Value {
return accumulator
}
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Array.reduceRight %q if not callable", call.Argument(0)))
}
2 changes: 1 addition & 1 deletion builtin_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func builtinDateToJSON(call FunctionCall) Value {
toISOString := obj.get("toISOString")
if !toISOString.isCallable() {
// FIXME
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Date.toJSON toISOString %q is not callable", toISOString))
}
return toISOString.call(call.runtime, objectValue(obj), []Value{})
}
Expand Down
2 changes: 1 addition & 1 deletion builtin_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func builtinNewError(obj *object, argumentList []Value) Value {
func builtinErrorToString(call FunctionCall) Value {
thisObject := call.thisObject()
if thisObject == nil {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Error.toString is nil"))
}

name := classErrorName
Expand Down
12 changes: 6 additions & 6 deletions builtin_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ func builtinFunctionToString(call FunctionCall) Value {
return stringValue(fn.node.source)
case bindFunctionObject:
return stringValue("function () { [native code] }")
default:
panic(call.runtime.panicTypeError("Function.toString unknown type %T", obj.value))
}

panic(call.runtime.panicTypeError("Function.toString()"))
}

func builtinFunctionApply(call FunctionCall) Value {
if !call.This.isCallable() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Function.apply %q is not callable", call.This))
}
this := call.Argument(0)
if this.IsUndefined() {
Expand All @@ -78,7 +78,7 @@ func builtinFunctionApply(call FunctionCall) Value {
return call.thisObject().call(this, nil, false, nativeFrame)
case valueObject:
default:
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Function.apply unknown type %T for second argument"))
}

arrayObject := argumentList.object()
Expand All @@ -93,7 +93,7 @@ func builtinFunctionApply(call FunctionCall) Value {

func builtinFunctionCall(call FunctionCall) Value {
if !call.This.isCallable() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Function.call %q is not callable", call.This))
}
thisObject := call.thisObject()
this := call.Argument(0)
Expand All @@ -110,7 +110,7 @@ func builtinFunctionCall(call FunctionCall) Value {
func builtinFunctionBind(call FunctionCall) Value {
target := call.This
if !target.isCallable() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Function.bind %q is not callable", call.This))
}
targetObject := target.object()

Expand Down
4 changes: 2 additions & 2 deletions builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ func builtinJSONStringify(call FunctionCall) Value {
}
valueJSON, err := json.Marshal(value)
if err != nil {
panic(call.runtime.panicTypeError(err.Error()))
panic(call.runtime.panicTypeError("JSON.stringify marshal: %s", err))
}
if ctx.gap != "" {
valueJSON1 := bytes.Buffer{}
if err = json.Indent(&valueJSON1, valueJSON, "", ctx.gap); err != nil {
panic(call.runtime.panicTypeError(err.Error()))
panic(call.runtime.panicTypeError("JSON.stringify indent: %s", err))
}
valueJSON = valueJSON1.Bytes()
}
Expand Down
35 changes: 16 additions & 19 deletions builtin_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func builtinObjectToString(call FunctionCall) Value {
func builtinObjectToLocaleString(call FunctionCall) Value {
toString := call.thisObject().get("toString")
if !toString.isCallable() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.toLocaleString %q is not callable", toString))
}
return toString.call(call.runtime, call.This)
}
Expand All @@ -90,7 +90,7 @@ func builtinObjectGetPrototypeOf(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.GetPrototypeOf is nil"))
}

if obj.prototype == nil {
Expand All @@ -104,7 +104,7 @@ func builtinObjectGetOwnPropertyDescriptor(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.GetOwnPropertyDescriptor is nil"))
}

name := call.Argument(1).string()
Expand All @@ -119,7 +119,7 @@ func builtinObjectDefineProperty(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.DefineProperty is nil"))
}
name := call.Argument(1).string()
descriptor := toPropertyDescriptor(call.runtime, call.Argument(2))
Expand All @@ -131,7 +131,7 @@ func builtinObjectDefineProperties(call FunctionCall) Value {
val := call.Argument(0)
obj := val.object()
if obj == nil {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.DefineProperties is nil"))
}

properties := call.runtime.toObject(call.Argument(1))
Expand All @@ -147,7 +147,7 @@ func builtinObjectDefineProperties(call FunctionCall) Value {
func builtinObjectCreate(call FunctionCall) Value {
prototypeValue := call.Argument(0)
if !prototypeValue.IsNull() && !prototypeValue.IsObject() {
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.Create is nil"))
}

obj := call.runtime.newObject()
Expand All @@ -171,17 +171,16 @@ func builtinObjectIsExtensible(call FunctionCall) Value {
if obj := val.object(); obj != nil {
return boolValue(obj.extensible)
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.IsExtensible is nil"))
}

func builtinObjectPreventExtensions(call FunctionCall) Value {
val := call.Argument(0)
if obj := val.object(); obj != nil {
obj.extensible = false
} else {
panic(call.runtime.panicTypeError())
return val
}
return val
panic(call.runtime.panicTypeError("Object.PreventExtensions is nil"))
}

func builtinObjectIsSealed(call FunctionCall) Value {
Expand All @@ -200,7 +199,7 @@ func builtinObjectIsSealed(call FunctionCall) Value {
})
return boolValue(result)
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.IsSealed is nil"))
}

func builtinObjectSeal(call FunctionCall) Value {
Expand All @@ -214,10 +213,9 @@ func builtinObjectSeal(call FunctionCall) Value {
return true
})
obj.extensible = false
} else {
panic(call.runtime.panicTypeError())
return val
}
return val
panic(call.runtime.panicTypeError("Object.Seal is nil"))
}

func builtinObjectIsFrozen(call FunctionCall) Value {
Expand All @@ -236,7 +234,7 @@ func builtinObjectIsFrozen(call FunctionCall) Value {
})
return boolValue(result)
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.IsFrozen is nil"))
}

func builtinObjectFreeze(call FunctionCall) Value {
Expand All @@ -259,10 +257,9 @@ func builtinObjectFreeze(call FunctionCall) Value {
return true
})
obj.extensible = false
} else {
panic(call.runtime.panicTypeError())
return val
}
return val
panic(call.runtime.panicTypeError("Object.Freeze is nil"))
}

func builtinObjectKeys(call FunctionCall) Value {
Expand All @@ -273,7 +270,7 @@ func builtinObjectKeys(call FunctionCall) Value {
})
return objectValue(call.runtime.newArrayOf(keys))
}
panic(call.runtime.panicTypeError())
panic(call.runtime.panicTypeError("Object.Keys is nil"))
}

func builtinObjectGetOwnPropertyNames(call FunctionCall) Value {
Expand Down
Loading

0 comments on commit ddcbf14

Please sign in to comment.