Skip to content

Commit 662186b

Browse files
authored
feat: include errors from the sdk in outputs of erroring hooks (#109)
1 parent d294a85 commit 662186b

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

internal/hooks/hook_executor_default.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ func (e *HookExecutorDefaultProtocol) Execute(ctx context.Context, opts HookExec
7272

7373
response := strings.TrimSpace(buffout.String())
7474
if err != nil {
75+
// Include stderr outputs in error details if these aren't streamed
76+
details := slackerror.ErrorDetails{}
77+
if opts.Stderr == nil {
78+
details = append(details, slackerror.ErrorDetail{Message: strings.TrimSpace(bufferr.String())})
79+
}
7580
return "", slackerror.New(slackerror.ErrSDKHookInvocationFailed).
76-
WithMessage("Command for '%s' returned an error: %s\n%s", opts.Hook.Name, err, strings.TrimSpace(bufferr.String()))
81+
WithMessage("Error running '%s' command: %s", opts.Hook.Name, err).
82+
WithDetails(details)
7783
}
7884

7985
// Special handling for the baseline protocol for the `start` hook

internal/hooks/hook_executor_default_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ func Test_Hook_Execute_Default_Protocol(t *testing.T) {
9090
},
9191
},
9292
expectedError: slackerror.New(slackerror.ErrSDKHookInvocationFailed).
93-
WithMessage("Command for 'sadpath' returned an error: explosion\nthere was a problem compiling your app"),
93+
WithMessage("Error running 'sadpath' command: explosion").
94+
WithDetails(slackerror.ErrorDetails{
95+
slackerror.ErrorDetail{
96+
Message: "there was a problem compiling your app",
97+
},
98+
}),
9499
expectedResponse: "",
95100
},
96101
"successful deploy command": {

internal/hooks/hook_executor_v2.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ func (e *HookExecutorMessageBoundaryProtocol) Execute(ctx context.Context, opts
8686

8787
cmd := opts.Exec.Command(cmdEnvVars, &stdout, stderr, opts.Stdin, cmdArgs[0], cmdArgVars...)
8888
if err = cmd.Run(); err != nil {
89+
// Include stderr outputs in error details if these aren't streamed
90+
details := slackerror.ErrorDetails{}
91+
if opts.Stderr == nil {
92+
details = append(details, slackerror.ErrorDetail{Message: strings.TrimSpace(bufferr.String())})
93+
}
8994
return "", slackerror.New(slackerror.ErrSDKHookInvocationFailed).
90-
WithMessage("Error running '%s' command: %s", opts.Hook.Name, err)
95+
WithMessage("Error running '%s' command: %s", opts.Hook.Name, err).
96+
WithDetails(details)
9197
}
9298
return buffout.String(), nil
9399
}

internal/hooks/hook_executor_v2_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,21 @@ func Test_Hook_Execute_V2_Protocol(t *testing.T) {
113113
Hook: HookScript{Command: "boom", Name: "sadpath"},
114114
Exec: &MockExec{
115115
mockCommand: &MockCommand{
116-
Err: errors.New("explosion"),
116+
Err: errors.New("explosion"),
117+
MockStderr: []byte("fireworks for the skies above"),
117118
},
118119
},
119120
},
120121
check: func(t *testing.T, response string, err error, mockExec ExecInterface) {
121122
require.Equal(t, slackerror.New(slackerror.ErrSDKHookInvocationFailed).
122-
WithMessage("Error running 'sadpath' command: explosion"), err)
123+
WithMessage("Error running 'sadpath' command: explosion").
124+
WithDetails(slackerror.ErrorDetails{
125+
slackerror.ErrorDetail{
126+
Message: "fireworks for the skies above",
127+
},
128+
}),
129+
err,
130+
)
123131
},
124132
},
125133
"fail to parse payload due to improper boundary strings": {

internal/shared/clients_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func Test_ClientFactory_InitSDKConfigFromJSON_brokenGetHooks(t *testing.T) {
234234
err := clients.InitSDKConfigFromJSON(ctx, []byte(getHooksJSON))
235235
require.Error(t, err)
236236
assert.Equal(t, slackerror.New(slackerror.ErrSDKHookInvocationFailed).Code, slackerror.ToSlackError(err).Code)
237-
assert.Contains(t, slackerror.ToSlackError(err).Message, "Command for 'GetHooks' returned an error")
237+
assert.Contains(t, slackerror.ToSlackError(err).Message, "Error running 'GetHooks' command")
238238
}
239239

240240
func Test_ClientFactory_InitSDKConfigFromJSON_brokenJSONFile(t *testing.T) {

0 commit comments

Comments
 (0)