Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instance,module) Improve error messages #42

Merged
merged 4 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ func NewInstanceWithImports(bytes []byte, imports *Imports) (Instance, error) {
)

if compileResult != cWasmerOk {
return nil, NewInstanceError("Failed to instantiate the module.")
var lastError, err = GetLastError()
var errorMessage = "Failed to instantiate the module:\n %s"

if err != nil {
errorMessage = fmt.Sprintf(errorMessage, "(unknown details)")
} else {
errorMessage = fmt.Sprintf(errorMessage, lastError)
}

return nil, NewInstanceError(errorMessage)
}

return instance, nil
Expand Down
12 changes: 11 additions & 1 deletion wasmer/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wasmer

import (
"fmt"
"io/ioutil"
"unsafe"
)
Expand Down Expand Up @@ -174,7 +175,16 @@ func (module *Module) InstantiateWithImports(imports *Imports) (Instance, error)
)

if instantiateResult != cWasmerOk {
return nil, NewModuleError("Failed to instantiate the module.")
var lastError, err = GetLastError()
var errorMessage = "Failed to instantiate the module:\n %s"

if err != nil {
errorMessage = fmt.Sprintf(errorMessage, "(unknown details)")
} else {
errorMessage = fmt.Sprintf(errorMessage, lastError)
}

return nil, NewModuleError(errorMessage)
}

return instance, nil
Expand Down
8 changes: 8 additions & 0 deletions wasmer/test/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ func TestModuleImport(t *testing.T) {
testModuleImport(t)
}

func TestInstanceImportMissingImports(t *testing.T) {
testInstanceImportMissingImports(t)
}

func TestModuleImportMissingImports(t *testing.T) {
testModuleImportMissingImports(t)
}

func TestImportNoAFunction(t *testing.T) {
_, err := wasm.NewImports().Append("sum", 42, unsafe.Pointer(nil))

Expand Down
15 changes: 15 additions & 0 deletions wasmer/test/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ func testModuleImport(t *testing.T) {
assert.NoError(t, err)
}

func testInstanceImportMissingImports(t *testing.T) {
_, err := wasm.NewInstance(getImportedFunctionBytes("examples", "imported_function.wasm"))

assert.EqualError(t, err, "Failed to instantiate the module:\n link error: Import not found, namespace: env, name: sum")
}

func testModuleImportMissingImports(t *testing.T) {
module, _ := wasm.Compile(getImportedFunctionBytes("examples", "imported_function.wasm"))
defer module.Close()

_, err := module.Instantiate()

assert.EqualError(t, err, "Failed to instantiate the module:\n error instantiating from module")
}

//export missingContext
func missingContext() int32 {
return 7
Expand Down
2 changes: 1 addition & 1 deletion wasmer/test/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestInstantiateInvalidModule(t *testing.T) {
instance, err := wasm.NewInstance(GetInvalidBytes())
defer instance.Close()

assert.EqualError(t, err, "Failed to instantiate the module.")
assert.EqualError(t, err, "Failed to instantiate the module:\n compile error: Validation error \"Invalid type\"")
}

func TestBasicSum(t *testing.T) {
Expand Down