Skip to content

Commit 116b578

Browse files
authored
Distinguish client and internal errors in action responses (#9162)
1 parent 2c2649b commit 116b578

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

pkg/actions/action.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ type MatchSpec struct {
6666
var (
6767
reName = regexp.MustCompile(`^\w[\w\-. ]+$`)
6868
reHookID = regexp.MustCompile(`^[_a-zA-Z][\-_a-zA-Z0-9]{1,255}$`)
69-
70-
ErrInvalidAction = errors.New("invalid action")
71-
ErrInvalidEventParameter = errors.New("invalid event parameter")
7269
)
7370

7471
func isEventSupported(event graveler.EventType) bool {

pkg/actions/errors.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package actions
2+
3+
import "errors"
4+
5+
var (
6+
ErrActionFailed = errors.New("action failed")
7+
ErrNotFound = errors.New("not found")
8+
ErrNilValue = errors.New("nil value")
9+
ErrIfExprNotBool = errors.New("hook 'if' expression should evaluate to a boolean")
10+
ErrParamConflict = errors.New("parameters conflict")
11+
ErrUnknownHookType = errors.New("unknown hook type")
12+
ErrInvalidAction = errors.New("invalid action")
13+
ErrInvalidEventParameter = errors.New("invalid event parameter")
14+
)

pkg/actions/hook.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package actions
33
import (
44
"bytes"
55
"context"
6-
"errors"
76
"fmt"
87
"net/http"
98

@@ -39,8 +38,6 @@ var hooks = map[HookType]NewHookFunc{
3938
HookTypeLua: NewLuaHook,
4039
}
4140

42-
var ErrUnknownHookType = errors.New("unknown hook type")
43-
4441
func NewHook(hook ActionHook, action *Action, cfg Config, server *http.Server, serverAddress string, collector stats.Collector) (Hook, error) {
4542
f := hooks[hook.Type]
4643
if f == nil {

pkg/actions/kv_run_results_iterator.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package actions
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76

87
"github.com/treeverse/lakefs/pkg/kv"
98
)
109

11-
var ErrParamConflict = errors.New("parameters conflict")
12-
1310
type KVRunResultIterator struct {
1411
it kv.MessageIterator
1512
err error

pkg/actions/service.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ const (
3232
commitPrefix = "commits"
3333
)
3434

35-
var (
36-
ErrNotFound = errors.New("not found")
37-
ErrNilValue = errors.New("nil value")
38-
ErrIfExprNotBool = errors.New("hook 'if' expression should evaluate to a boolean")
39-
)
40-
4135
type Config struct {
4236
Enabled bool
4337
Lua struct {
@@ -366,6 +360,10 @@ func (s *StoreService) runTasks(ctx context.Context, record graveler.HookRecord,
366360
var buf bytes.Buffer
367361
if task.Err == nil {
368362
task.Err = task.Hook.Run(ctx, record, &buf)
363+
if task.Err != nil {
364+
// Wrap the error to later indicate that the precondition failed due to a client-side hook error.
365+
task.Err = fmt.Errorf("%w: %s", ErrActionFailed, task.Err)
366+
}
369367
}
370368
task.EndTime = time.Now().UTC()
371369

pkg/api/controller.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,14 +2934,6 @@ func (c *Controller) handleAPIErrorCallback(ctx context.Context, w http.Response
29342934

29352935
log := c.Logger.WithContext(ctx).WithError(err)
29362936

2937-
// Handle Hook Errors
2938-
var hookAbortErr *graveler.HookAbortError
2939-
if errors.As(err, &hookAbortErr) {
2940-
log.WithField("run_id", hookAbortErr.RunID).Warn("aborted by hooks")
2941-
cb(w, r, http.StatusPreconditionFailed, hookAbortErr.Unwrap())
2942-
return true
2943-
}
2944-
29452937
// order of case is important, more specific errors should be first
29462938
switch {
29472939
case errors.Is(err, graveler.ErrLinkAddressInvalid),
@@ -3027,6 +3019,9 @@ func (c *Controller) handleAPIErrorCallback(ctx context.Context, w http.Response
30273019
case errors.Is(err, authentication.ErrInsufficientPermissions):
30283020
c.Logger.WithContext(ctx).WithError(err).Info("User verification failed - insufficient permissions")
30293021
cb(w, r, http.StatusUnauthorized, http.StatusText(http.StatusUnauthorized))
3022+
case errors.Is(err, actions.ErrActionFailed):
3023+
log.WithError(err).Debug("Precondition failed, aborted by action failure")
3024+
cb(w, r, http.StatusPreconditionFailed, err)
30303025
default:
30313026
c.Logger.WithContext(ctx).WithError(err).Error("API call returned status internal server error")
30323027
cb(w, r, http.StatusInternalServerError, err)

0 commit comments

Comments
 (0)