Skip to content

Commit

Permalink
fix: error handling (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleFromKitware authored Oct 27, 2023
1 parent bb5f31e commit 622336b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions transfer/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ var (
ErrInvalidPacket = errors.New("invalid packet")
// ErrNotFound is the not found error.
ErrNotFound = errors.New("not found")
// ErrUnauthorized is the unauthorized error.
ErrUnauthorized = errors.New("unauthorized")
// ErrUnauthorized is the forbidden error.
ErrForbidden = errors.New("forbidden")
)
26 changes: 21 additions & 5 deletions transfer/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *Processor) BatchData(op string, presentAction string, missingAction str
}
batch, err := p.ReadBatch(op, args)
if err != nil {
return p.Error(StatusBadRequest, err.Error(), ArgsToList(args)...)
return nil, err
}
oids := make([]string, 0)
for _, item := range batch {
Expand Down Expand Up @@ -425,19 +425,19 @@ func (p *Processor) ProcessCommands(op string) error {
if len(msgs) > 1 {
status, err = p.PutObject(msgs[1])
} else {
err = p.handler.SendError(StatusForbidden, "not allowed")
err = p.handler.SendError(StatusBadRequest, "bad request")
}
case verifyObjectCommand:
if len(msgs) > 1 {
status, err = p.VerifyObject(msgs[1])
} else {
err = p.handler.SendError(StatusForbidden, "not allowed")
err = p.handler.SendError(StatusBadRequest, "bad request")
}
case getObjectCommand:
if len(msgs) > 1 {
status, err = p.GetObject(msgs[1])
} else {
err = p.handler.SendError(StatusForbidden, "not allowed")
err = p.handler.SendError(StatusBadRequest, "bad request")
}
case lockCommand:
status, err = p.Lock()
Expand Down Expand Up @@ -466,12 +466,28 @@ func (p *Processor) ProcessCommands(op string) error {
if err != nil {
switch {
case errors.Is(err, ErrExtraData),
errors.Is(err, ErrNotAllowed),
errors.Is(err, ErrParseError),
errors.Is(err, ErrInvalidPacket),
errors.Is(err, ErrCorruptData):
if err := p.handler.SendError(StatusBadRequest, fmt.Errorf("error: %w", err).Error()); err != nil {
p.logger.Log("failed to send pktline", "err", err)
}
case errors.Is(err, ErrNotAllowed):
if err := p.handler.SendError(StatusMethodNotAllowed, fmt.Errorf("error: %w", err).Error()); err != nil {
p.logger.Log("failed to send pktline", "err", err)
}
case errors.Is(err, ErrNotFound):
if err := p.handler.SendError(StatusNotFound, fmt.Errorf("error: %w", err).Error()); err != nil {
p.logger.Log("failed to send pktline", "err", err)
}
case errors.Is(err, ErrUnauthorized):
if err := p.handler.SendError(StatusUnauthorized, fmt.Errorf("error: %w", err).Error()); err != nil {
p.logger.Log("failed to send pktline", "err", err)
}
case errors.Is(err, ErrForbidden):
if err := p.handler.SendError(StatusForbidden, fmt.Errorf("error: %w", err).Error()); err != nil {
p.logger.Log("failed to send pktline", "err", err)
}
default:
p.logger.Log("failed to process command", "err", err)
if err := p.handler.SendError(StatusInternalServerError, "internal error"); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions transfer/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ const (
StatusBadRequest uint32 = http.StatusBadRequest
StatusForbidden uint32 = http.StatusForbidden
StatusNotFound uint32 = http.StatusNotFound
StatusMethodNotAllowed uint32 = http.StatusMethodNotAllowed
StatusConflict uint32 = http.StatusConflict
StatusInternalServerError uint32 = http.StatusInternalServerError
StatusUnauthorized uint32 = http.StatusUnauthorized
)

// StatusString returns the status string lowercased for a status code.
Expand Down

0 comments on commit 622336b

Please sign in to comment.