Skip to content

Commit

Permalink
fix: add more context to errors
Browse files Browse the repository at this point in the history
Signed-off-by: Soumil Paranjpay <[email protected]>
  • Loading branch information
Soumil-07 committed Dec 13, 2023
1 parent ef4a44c commit bafd6d8
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions transformer/external/wasmtransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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)

Expand Down Expand Up @@ -174,33 +175,33 @@ 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
transformOutput, err := vm.Execute("transform", dataPointer)
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
Expand All @@ -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
Expand Down

0 comments on commit bafd6d8

Please sign in to comment.