Skip to content

Commit

Permalink
error cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
James Lawrence authored and james-lawrence committed Apr 2, 2019
1 parent e887fc9 commit 9c98723
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
3 changes: 1 addition & 2 deletions dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package slack
import (
"context"
"encoding/json"
"errors"
)

// InputType is the type of the dialog input type
Expand Down Expand Up @@ -89,7 +88,7 @@ func (api *Client) OpenDialog(triggerID string, dialog Dialog) (err error) {
// EXPERIMENTAL: dialog functionality is currently experimental, api is not considered stable.
func (api *Client) OpenDialogContext(ctx context.Context, triggerID string, dialog Dialog) (err error) {
if triggerID == "" {
return errors.New("received empty parameters")
return ErrParametersMissing
}

req := DialogTrigger{
Expand Down
17 changes: 17 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package slack

import "github.com/nlopes/slack/internal/errorsx"

// Errors returned by various methods.
const (
ErrAlreadyDisconnected = errorsx.String("Invalid call to Disconnect - Slack API is already disconnected")
ErrParametersMissing = errorsx.String("received empty parameters")
ErrInvalidConfiguration = errorsx.String("invalid configuration")
ErrMissingHeaders = errorsx.String("missing headers")
ErrExpiredTimestamp = errorsx.String("timestamp is too old")
)

// internal errors
const (
errPaginationComplete = errorsx.String("pagination complete")
)
3 changes: 1 addition & 2 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package slack

import (
"context"
"errors"
"fmt"
"io"
"net/url"
Expand Down Expand Up @@ -292,7 +291,7 @@ func (api *Client) DeleteFileComment(commentID, fileID string) error {
// DeleteFileCommentContext deletes a file's comment with a custom context
func (api *Client) DeleteFileCommentContext(ctx context.Context, fileID, commentID string) (err error) {
if fileID == "" || commentID == "" {
return errors.New("received empty parameters")
return ErrParametersMissing
}

values := url.Values{
Expand Down
8 changes: 8 additions & 0 deletions internal/errorsx/errorsx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package errorsx

// String representing an error, useful for declaring string constants as errors.
type String string

func (t String) Error() string {
return string(t)
}
6 changes: 0 additions & 6 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,6 @@ func okJSONHandler(rw http.ResponseWriter, r *http.Request) {
rw.Write(response)
}

type errorString string

func (t errorString) Error() string {
return string(t)
}

// timerReset safely reset a timer, see time.Timer.Reset for details.
func timerReset(t *time.Timer, d time.Duration) {
if !t.Stop() {
Expand Down
5 changes: 2 additions & 3 deletions security.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"net/http"
Expand Down Expand Up @@ -34,7 +33,7 @@ func unsafeSignatureVerifier(header http.Header, secret string) (_ SecretsVerifi
stimestamp := header.Get(hTimestamp)

if signature == "" || stimestamp == "" {
return SecretsVerifier{}, errors.New("missing headers")
return SecretsVerifier{}, ErrMissingHeaders
}

if bsignature, err = hex.DecodeString(strings.TrimPrefix(signature, "v0=")); err != nil {
Expand Down Expand Up @@ -70,7 +69,7 @@ func NewSecretsVerifier(header http.Header, secret string) (sv SecretsVerifier,

diff := absDuration(time.Since(time.Unix(timestamp, 0)))
if diff > 5*time.Minute {
return SecretsVerifier{}, fmt.Errorf("timestamp is too old")
return SecretsVerifier{}, ErrExpiredTimestamp
}

return sv, err
Expand Down
1 change: 0 additions & 1 deletion users.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const (
DEFAULT_USER_PHOTO_CROP_X = -1
DEFAULT_USER_PHOTO_CROP_Y = -1
DEFAULT_USER_PHOTO_CROP_W = -1
errPaginationComplete = errorString("pagination complete")
)

// UserProfile contains all the information details of a given user
Expand Down

0 comments on commit 9c98723

Please sign in to comment.