3

I'm trying to "force" my Go system to behave in a particular manner to make my "testing" easier.

I have the following code snippet:

func SendTokenProcessFromModel(process model.Process) (*SendTokenProcess, error) {
    cdc := codec.New()
    var msgSend bank.MsgSend
    if err := cdc.UnmarshalJSON(process.RawMessage, &msgSend); err != nil {
        return nil, fmt.Errorf("could not unmarshal a MsgSend: %w", err)
    }
    ...
}

In which I would like to set the err variable to true to force my function to stop and, as consequence, the action to fail.

I have come across 2x approaches to set a variable whilst debugging.

  • golang/vscode-go #1173
    Where they suggest that you set the variable using the VARIABLES tab in the debugger.

    I cannot do this as the err variable from my above snippet isn't present... the only variables present are the 3x others, namely: cdc, process & msgSend - assuming that our Breakpoint has been inserted at line "4": if err := cdc...

  • go-delve/delve #826
    Where it is suggested to use the dlv CLI and to perform a command such as that outlined below:

(dlv) set err = "true"

       Due to the debugging session being instantiated by VS Code I cant access the (dlv) commands.
       I did try typing it directly into the DEBUG CONSOLE with no success, only error messages.
       I'm not saying this can't be done, just that I don't know how should it be possible.

From option 1 they also nention the below:

err := "true"

But, again, not sure where, or how, to implement this.

Any help, guidance or support is appreciated as always.

Kind regards,
Ben

Ben
  • 41
  • 8

1 Answers1

0

Don't know if the VSCode Debug Console has changed much, but here's what I worked out.

> set err=nil
Unable to evaluate expression: 1:5: expected 'EOF', found err
> set err nil
Unable to evaluate expression: 1:5: expected 'EOF', found err

> call err = nil
error nil
> call err = errors.New("Error")
> err
error(*errors.errorString) *{s: "Error"}

Non-spacing seems to be tolerated. I also had no problem with call err=nil.

Joe
  • 2,946
  • 18
  • 17