diff --git a/transformer/external/wasmtransformer.go b/transformer/external/wasmtransformer.go index 9215e2d66..5eb853a3e 100644 --- a/transformer/external/wasmtransformer.go +++ b/transformer/external/wasmtransformer.go @@ -82,19 +82,19 @@ func (t *WASM) GetConfig() (transformertypes.Transformer, *environment.Environme func (t *WASM) DirectoryDetect(dir string) (map[string][]transformertypes.Artifact, error) { vm, err := t.initVm([]string{dir + ":" + dir}) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to initialize WASM VM: %w", err) } allocateResult, err := vm.Execute("malloc", int32(len(dir)+1)) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to alloc memory for directory: %w", err) } dirPointer := allocateResult[0].(int32) mod := vm.GetActiveModule() mem := mod.FindMemory("memory") memData, err := mem.GetData(uint(dirPointer), uint(len(dir)+1)) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to load wasm memory region: %w", err) } copy(memData, dir) memData[len(dir)] = 0 @@ -106,19 +106,20 @@ func (t *WASM) DirectoryDetect(dir string) (map[string][]transformertypes.Artifa directoryDetectOutputPointer := directoryDetectOutput[0].(int32) memData, err = mem.GetData(uint(directoryDetectOutputPointer), 8) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to load directoryDetect output: %w", err) } resultPointer := binary.LittleEndian.Uint32(memData[:4]) resultLength := binary.LittleEndian.Uint32(memData[4:]) memData, err = mem.GetData(uint(resultPointer), uint(resultLength)) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to read directoryDetect output: %w", err) } - logrus.Debug(string(memData)) services := map[string][]transformertypes.Artifact{} - // will handles errors err = json.Unmarshal(memData, &services) + if err != nil { + err = fmt.Errorf("failed to unmarshal directoryDetect output: %w", err) + } return services, err } @@ -131,7 +132,7 @@ func (t *WASM) Transform(newArtifacts []transformertypes.Artifact, alreadySeenAr data["oldArtifacts"] = alreadySeenArtifacts dataByt, err := json.Marshal(data) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to marshal transform input: %w", err) } dataStr := string(dataByt) @@ -174,19 +175,19 @@ func (t *WASM) Transform(newArtifacts []transformertypes.Artifact, alreadySeenAr vm, err := t.initVm(finalPreopens) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to initialize VM (transform): %w", err) } allocateResult, err := vm.Execute("malloc", int32(len(dataStr)+1)) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to alloc memory for transform input: %w", err) } dataPointer := allocateResult[0].(int32) mod := vm.GetActiveModule() mem := mod.FindMemory("memory") memData, err := mem.GetData(uint(dataPointer), uint(len(dataStr)+1)) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to load wasm memory region: %w", err) } copy(memData, dataStr) memData[len(dataStr)] = 0 @@ -194,13 +195,13 @@ func (t *WASM) Transform(newArtifacts []transformertypes.Artifact, alreadySeenAr transformOutputPointer := transformOutput[0].(int32) memData, err = mem.GetData(uint(transformOutputPointer), 8) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to load transform output: %w", err) } resultPointer := binary.LittleEndian.Uint32(memData[:4]) resultLength := binary.LittleEndian.Uint32(memData[4:]) memData, err = mem.GetData(uint(resultPointer), uint(resultLength)) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to read transform output: %w", err) } logrus.Debug(string(memData)) var output transformertypes.TransformOutput @@ -227,19 +228,19 @@ func (t *WASM) initVm(preopens []string) (*wasmedge.VM, error) { err := vm.LoadWasmFile(filepath.Join(t.Env.GetEnvironmentContext(), t.WASMConfig.WASMModule)) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to load wasm module %s: %w", t.WASMConfig.WASMModule, err) } err = vm.Validate() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to validate VM: %w", err) } err = vm.Instantiate() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to instantiate VM: %w", err) } _, err = vm.Execute("_start") if err != nil { - return nil, err + return nil, fmt.Errorf("failed to execute _start: %w", err) } return vm, nil