Skip to content

Commit

Permalink
implement error typing and api check for manual interaction storage i…
Browse files Browse the repository at this point in the history
…face
  • Loading branch information
lucamrgs committed Jan 20, 2025
1 parent d404d3c commit 5a4f8af
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
20 changes: 17 additions & 3 deletions pkg/api/manual/manual_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manual
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"reflect"
Expand Down Expand Up @@ -107,7 +108,11 @@ func (manualHandler *ManualHandler) GetPendingCommand(g *gin.Context) {
commandData, err := manualHandler.interactionCapability.GetPendingCommand(executionMetadata)
if err != nil {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusInternalServerError,
code := http.StatusBadRequest
if errors.Is(err, manual.ErrorPendingCommandNotFound{}) {
code = http.StatusNotFound
}
apiError.SendErrorResponse(g, code,
"Failed to provide pending manual command",
"GET /manual/"+execution_id+"/"+step_id, "")
return
Expand Down Expand Up @@ -195,8 +200,17 @@ func (manualHandler *ManualHandler) PostContinue(g *gin.Context) {
err = manualHandler.interactionCapability.PostContinue(interactionResponse)
if err != nil {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusInternalServerError,
"Failed to post continue ID",
code := http.StatusBadRequest
msg := "Failed to post continue ID"
if errors.Is(err, manual.ErrorPendingCommandNotFound{}) {
code = http.StatusNotFound
msg = "Pending command not found"
} else if errors.Is(err, manual.ErrorNonMatchingOutArgs{}) {
code = http.StatusBadRequest
msg = "Provided out args don't match with expected"
}
apiError.SendErrorResponse(g, code,
msg,
"POST /manual/continue", "")
return
}
Expand Down
11 changes: 7 additions & 4 deletions pkg/core/capability/manual/interaction/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,17 @@ func (manualController *InteractionController) getAllPendingCommandsInfo() []man
func (manualController *InteractionController) getPendingInteraction(commandMetadata execution.Metadata) (manual.InteractionStorageEntry, error) {
executionCommands, ok := manualController.InteractionStorage[commandMetadata.ExecutionId.String()]
if !ok {
err := fmt.Errorf("no pending commands found for execution %s", commandMetadata.ExecutionId.String())
return manual.InteractionStorageEntry{}, err
err := fmt.Sprintf("no pending commands found for execution %s", commandMetadata.ExecutionId.String())
return manual.InteractionStorageEntry{}, manual.ErrorPendingCommandNotFound{Err: err}
}
interaction, ok := executionCommands[commandMetadata.StepId]
if !ok {
err := fmt.Errorf("no pending commands found for execution %s -> step %s",
err := fmt.Sprintf("no pending commands found for execution %s -> step %s",
commandMetadata.ExecutionId.String(),
commandMetadata.StepId,
)
return manual.InteractionStorageEntry{}, err
return manual.InteractionStorageEntry{}, manual.ErrorPendingCommandNotFound{Err: err}

}
return interaction, nil
}
Expand Down Expand Up @@ -251,6 +252,8 @@ func (manualController *InteractionController) validateMatchingOutArgs(pendingEn
// first check that out args provided match the variables
if _, ok := pendingEntry.CommandInfo.OutArgsVariables[varName]; !ok {
err = fmt.Errorf("provided out arg %s does not match any intended out arg", varName)
return warns, manual.ErrorNonMatchingOutArgs{Err: err.Error()}

}
// then warn if any value outside "value" has changed
if pending, ok := pendingEntry.CommandInfo.OutArgsVariables[varName]; ok {
Expand Down
27 changes: 14 additions & 13 deletions pkg/core/capability/manual/interaction/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ func TestPostContinueFailOnNonexistingVariable(t *testing.T) {

err = interaction.PostContinue(outArgsUpdate)

expectedErr := fmt.Errorf("provided out arg %s does not match any intended out arg", outArg.Name)
expectedErr := manualModel.ErrorNonMatchingOutArgs{
Err: fmt.Sprintf("provided out arg %s does not match any intended out arg", outArg.Name)}

assert.Equal(t, err, expectedErr)
}
Expand Down Expand Up @@ -424,10 +425,10 @@ func TestGetEmptyPendingCommand(t *testing.T) {
t.Fail()
}

expectedErr := errors.New(
"no pending commands found for execution " +
expectedErr := manualModel.ErrorPendingCommandNotFound{
Err: "no pending commands found for execution " +
"61a6c41e-6efc-4516-a242-dfbc5c89d562",
)
}

assert.Equal(t, emptyCommandInfo, manualModel.CommandInfo{})
assert.Equal(t, err, expectedErr)
Expand Down Expand Up @@ -473,9 +474,9 @@ func TestFailOnRetrieveUnexistingExecutionInteraction(t *testing.T) {
t.Fail()
}

expectedErr := errors.New(
"no pending commands found for execution 50b6d52c-6efc-4516-a242-dfbc5c89d421",
)
expectedErr := manualModel.ErrorPendingCommandNotFound{
Err: "no pending commands found for execution 50b6d52c-6efc-4516-a242-dfbc5c89d421",
}
assert.Equal(t, err, expectedErr)
}

Expand All @@ -500,11 +501,11 @@ func TestFailOnRetrieveNonExistingCommandInteraction(t *testing.T) {
t.Fail()
}

expectedErr := errors.New(
"no pending commands found for execution " +
expectedErr := manualModel.ErrorPendingCommandNotFound{
Err: "no pending commands found for execution " +
"61a6c41e-6efc-4516-a242-dfbc5c89d562 -> " +
"step 50b6d52c-6efc-4516-a242-dfbc5c89d421",
)
}
assert.Equal(t, err, expectedErr)
}

Expand Down Expand Up @@ -545,10 +546,10 @@ func TestRemovePendingInteraciton(t *testing.T) {
t.Fail()
}

expectedErr := errors.New(
"no pending commands found for execution " +
expectedErr := manualModel.ErrorPendingCommandNotFound{
Err: "no pending commands found for execution " +
"61a6c41e-6efc-4516-a242-dfbc5c89d562",
)
}
assert.Equal(t, err, expectedErr)
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/models/manual/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,20 @@ type ManualCapabilityCommunication struct {
TimeoutContext context.Context
Channel chan InteractionResponse
}

// Errors #####################################################################
type ErrorPendingCommandNotFound struct {
Err string
}

type ErrorNonMatchingOutArgs struct {
Err string
}

func (e ErrorPendingCommandNotFound) Error() string {
return e.Err
}

func (e ErrorNonMatchingOutArgs) Error() string {
return e.Err
}

0 comments on commit 5a4f8af

Please sign in to comment.