Skip to content

Commit

Permalink
Merge abb7f66 into 31820f3
Browse files Browse the repository at this point in the history
  • Loading branch information
oklahomer committed Aug 30, 2020
2 parents 31820f3 + abb7f66 commit 7e8a0c6
Show file tree
Hide file tree
Showing 31 changed files with 144 additions and 129 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Expand Up @@ -2,9 +2,9 @@ language: go
sudo: false

go:
- "1.12"
- "1.13"
- "1.14"
- "1.15"
- "tip"

before_install:
Expand All @@ -22,6 +22,3 @@ matrix:

git:
depth: 10

env:
- GO111MODULE=on
11 changes: 9 additions & 2 deletions alert.go
Expand Up @@ -2,7 +2,7 @@ package sarah

import (
"context"
"golang.org/x/xerrors"
"fmt"
"strings"
)

Expand Down Expand Up @@ -46,7 +46,14 @@ func (a *alerters) alertAll(ctx context.Context, botType BotType, err error) err
func() {
defer func() {
if r := recover(); r != nil {
alertErrs.appendError(xerrors.Errorf("panic on Alerter.Alert: %w", r))
e, ok := r.(error)
var err error
if ok {
err = fmt.Errorf("panic on Alerter.Alert: %w", e)
} else {
err = fmt.Errorf("panic on Alerter.Alert: %+v", r)
}
alertErrs.appendError(err)
}
}()
err := alerter.Alert(ctx, botType, err)
Expand Down
20 changes: 17 additions & 3 deletions alert_test.go
Expand Up @@ -78,7 +78,13 @@ func TestAlerters_alertAll(t *testing.T) {
t.Errorf("Expected no error to be returned, but got %s.", err.Error())
}

wrappedErr := errors.New("panic with an error")
a = &alerters{
&DummyAlerter{
AlertFunc: func(_ context.Context, _ BotType, _ error) error {
panic(wrappedErr)
},
},
&DummyAlerter{
AlertFunc: func(_ context.Context, _ BotType, _ error) error {
panic("PANIC!!")
Expand All @@ -100,9 +106,17 @@ func TestAlerters_alertAll(t *testing.T) {
if err == nil {
t.Fatal("Expected error to be returned")
}
if e, ok := err.(*alertErrs); !ok {

typed, ok := err.(*alertErrs)
if !ok {
t.Fatalf("Expected error type of *alertErrs, but was %T.", err)
} else if len(*e) != 2 {
t.Errorf("Expected 2 errors to be stored: %#v.", err)
}
if len(*typed) != 3 {
t.Fatalf("Expected 3 errors to be stored: %#v.", err)
}

e := errors.Unwrap((*typed)[0])
if e != wrappedErr {
t.Errorf("Expected error is not wrapped: %+v", e)
}
}
3 changes: 1 addition & 2 deletions alerter/line/client.go
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"fmt"
"github.com/oklahomer/go-sarah/v3"
"golang.org/x/xerrors"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (c *Client) Alert(ctx context.Context, botType sarah.BotType, err error) er
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("response status %d is returned", resp.StatusCode)
return fmt.Errorf("response status %d is returned", resp.StatusCode)
}

return nil
Expand Down
11 changes: 6 additions & 5 deletions command.go
Expand Up @@ -2,8 +2,9 @@ package sarah

import (
"context"
"errors"
"fmt"
"github.com/oklahomer/go-sarah/v3/log"
"golang.org/x/xerrors"
"reflect"
"regexp"
"strings"
Expand All @@ -13,7 +14,7 @@ import (
var (
// ErrCommandInsufficientArgument depicts an error that not enough arguments are set to CommandProps.
// This is returned on CommandProps.Build() inside of runner.Run()
ErrCommandInsufficientArgument = xerrors.New("BotType, Identifier, InstructionFunc, MatchFunc and (Configurable)Func must be set.")
ErrCommandInsufficientArgument = errors.New("BotType, Identifier, InstructionFunc, MatchFunc and (Configurable)Func must be set")
)

// CommandResponse is returned by Command or Task when the execution is finished.
Expand Down Expand Up @@ -118,9 +119,9 @@ func buildCommand(ctx context.Context, props *CommandProps, watcher ConfigWatche
}()

var notFoundErr *ConfigNotFoundError
if err != nil && !xerrors.As(err, &notFoundErr) {
if err != nil && !errors.As(err, &notFoundErr) {
// Unacceptable error
return nil, xerrors.Errorf("failed to read config for %s:%s: %w", props.botType, props.identifier, err)
return nil, fmt.Errorf("failed to read config for %s:%s: %w", props.botType, props.identifier, err)
}

return &defaultCommand{
Expand Down Expand Up @@ -361,7 +362,7 @@ func (builder *CommandPropsBuilder) Build() (*CommandProps, error) {
func (builder *CommandPropsBuilder) MustBuild() *CommandProps {
props, err := builder.Build()
if err != nil {
panic(xerrors.Errorf("error on building CommandProps: %w", err))
panic(fmt.Errorf("error on building CommandProps: %w", err))
}

return props
Expand Down
17 changes: 9 additions & 8 deletions command_test.go
Expand Up @@ -2,7 +2,8 @@ package sarah

import (
"context"
"golang.org/x/xerrors"
"errors"
"fmt"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -489,11 +490,11 @@ func Test_buildCommand(t *testing.T) {
validateConfig: func(cfg interface{}) error {
config, ok := cfg.(*config)
if !ok {
return xerrors.Errorf("Unexpected type is passed: %T.", cfg)
return fmt.Errorf("unexpected type is passed: %T", cfg)
}

if config.text != "texts" {
return xerrors.Errorf("Unexpected value is set: %s", config.text)
return fmt.Errorf("nexpected value is set: %s", config.text)
}

return nil
Expand Down Expand Up @@ -531,11 +532,11 @@ func Test_buildCommand(t *testing.T) {
validateConfig: func(cfg interface{}) error {
config, ok := cfg.(config) // Value is passed
if !ok {
return xerrors.Errorf("Unexpected type is passed: %T.", cfg)
return fmt.Errorf("unexpected type is passed: %T", cfg)
}

if config.text != "texts" {
return xerrors.Errorf("Unexpected value is set: %s", config.text)
return fmt.Errorf("unexpected value is set: %s", config.text)
}

return nil
Expand Down Expand Up @@ -568,11 +569,11 @@ func Test_buildCommand(t *testing.T) {
validateConfig: func(cfg interface{}) error {
config, ok := cfg.(*config)
if !ok {
return xerrors.Errorf("Unexpected type is passed: %T.", cfg)
return fmt.Errorf("unexpected type is passed: %T", cfg)
}

if config.text != "" {
return xerrors.Errorf("Unexpected value is set: %s", config.text)
return fmt.Errorf("unexpected value is set: %s", config.text)
}

return nil
Expand All @@ -596,7 +597,7 @@ func Test_buildCommand(t *testing.T) {
},
watcher: &DummyConfigWatcher{
ReadFunc: func(_ context.Context, _ BotType, _ string, _ interface{}) error {
return xerrors.New("unacceptable error")
return errors.New("unacceptable error")
},
},
hasErr: true,
Expand Down
15 changes: 7 additions & 8 deletions examples/simple/plugins/worldweather/client.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"golang.org/x/xerrors"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -47,7 +46,7 @@ func (client *Client) buildEndpoint(apiType string, queryParams *url.Values) *ur

requestURL, err := url.Parse(fmt.Sprintf(weatherAPIEndpointFormat, apiType))
if err != nil {
panic(xerrors.Errorf("failed to parse construct URL for %s: %w", apiType, err))
panic(fmt.Errorf("failed to parse construct URL for %s: %w", apiType, err))
}
requestURL.RawQuery = queryParams.Encode()

Expand All @@ -59,26 +58,26 @@ func (client *Client) Get(ctx context.Context, apiType string, queryParams *url.
endpoint := client.buildEndpoint(apiType, queryParams)
req, err := http.NewRequest(http.MethodGet, endpoint.String(), nil)
if err != nil {
return xerrors.Errorf("failed to build request: %w", err)
return fmt.Errorf("failed to build request: %w", err)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return xerrors.Errorf("failed on GET request for %s: %w", apiType, err)
return fmt.Errorf("failed on GET request for %s: %w", apiType, err)
}

defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("response status %d is returned", resp.StatusCode)
return fmt.Errorf("response status %d is returned", resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return xerrors.Errorf("failed to read response: %w", err)
return fmt.Errorf("failed to read response: %w", err)
}

if err := json.Unmarshal(body, data); err != nil {
return xerrors.Errorf("failed to parse returned json data: %w", err)
return fmt.Errorf("failed to parse returned json data: %w", err)
}

return nil
Expand All @@ -90,7 +89,7 @@ func (client *Client) LocalWeather(ctx context.Context, location string) (*Local
queryParams.Add("q", location)
data := &LocalWeatherResponse{}
if err := client.Get(ctx, "weather", queryParams, data); err != nil {
return nil, xerrors.Errorf("failed getting weather data: %w", err)
return nil, fmt.Errorf("failed getting weather data: %w", err)
}

return data, nil
Expand Down
3 changes: 1 addition & 2 deletions examples/simple/plugins/worldweather/client_test.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"errors"
"golang.org/x/xerrors"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -186,7 +185,7 @@ func TestClient_GetRequestError(t *testing.T) {
}

var urlErr *url.Error
if !xerrors.As(err, &urlErr) {
if !errors.As(err, &urlErr) {
t.Errorf("Unexpected error is returned: %#v.", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/status/config.go
@@ -1,18 +1,18 @@
package main

import (
"fmt"
"github.com/oklahomer/go-sarah/v3"
"github.com/oklahomer/go-sarah/v3/slack"
"github.com/oklahomer/go-sarah/v3/workers"
"golang.org/x/xerrors"
"gopkg.in/yaml.v2"
"io/ioutil"
)

func readConfig(path string) (*config, error) {
body, err := ioutil.ReadFile(path)
if err != nil {
return nil, xerrors.Errorf("failed to read file: %w", err)
return nil, fmt.Errorf("failed to read file: %w", err)
}

// Populate with default configuration value by calling each constructor.
Expand All @@ -24,7 +24,7 @@ func readConfig(path string) (*config, error) {
}
err = yaml.Unmarshal(body, c)
if err != nil {
return nil, xerrors.Errorf("failed to read yaml: %w", err)
return nil, fmt.Errorf("failed to read yaml: %w", err)
}

return c, nil
Expand Down
6 changes: 3 additions & 3 deletions examples/status/main.go
Expand Up @@ -9,11 +9,11 @@ package main
import (
"context"
"flag"
"fmt"
"github.com/oklahomer/go-sarah/v3"
"github.com/oklahomer/go-sarah/v3/log"
"github.com/oklahomer/go-sarah/v3/slack"
"github.com/oklahomer/go-sarah/v3/workers"
"golang.org/x/xerrors"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -80,11 +80,11 @@ func setupSlackBot(cfg *config) (sarah.Bot, error) {
storage := sarah.NewUserContextStorage(cfg.ContextCache)
slackAdapter, err := slack.NewAdapter(cfg.Slack)
if err != nil {
return nil, xerrors.Errorf("failed to initialize Slack adapter: %w", err)
return nil, fmt.Errorf("failed to initialize Slack adapter: %w", err)
}
slackBot, err := sarah.NewBot(slackAdapter, sarah.BotWithStorage(storage))
if err != nil {
return nil, xerrors.Errorf("failed to initialize Bot with given Slack adapter: %w", err)
return nil, fmt.Errorf("failed to initialize Bot with given Slack adapter: %w", err)
}
return slackBot, nil
}
9 changes: 5 additions & 4 deletions gitter/adapter.go
Expand Up @@ -2,10 +2,11 @@ package gitter

import (
"context"
"errors"
"fmt"
"github.com/oklahomer/go-sarah/v3"
"github.com/oklahomer/go-sarah/v3/log"
"github.com/oklahomer/go-sarah/v3/retry"
"golang.org/x/xerrors"
)

const (
Expand Down Expand Up @@ -119,21 +120,21 @@ func receiveMessageRecursive(messageReceiver MessageReceiver, enqueueInput func(
message, err := messageReceiver.Receive()

var malformedErr *MalformedPayloadError
if xerrors.Is(err, ErrEmptyPayload) {
if errors.Is(err, ErrEmptyPayload) {
// https://developer.gitter.im/docs/streaming-api
// Parsers must be tolerant of occasional extra newline characters placed between messages.
// These characters are sent as periodic "keep-alive" messages to tell clients and NAT firewalls
// that the connection is still alive during low message volume periods.
continue

} else if xerrors.As(err, &malformedErr) {
} else if errors.As(err, &malformedErr) {
log.Warnf("Skipping malformed input: %+v", err)
continue

} else if err != nil {
// At this point, assume connection is unstable or is closed.
// Let caller proceed to reconnect or quit.
return xerrors.Errorf("failed to receive input: %w", err)
return fmt.Errorf("failed to receive input: %w", err)

}

Expand Down
4 changes: 2 additions & 2 deletions gitter/connection.go
Expand Up @@ -4,16 +4,16 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/oklahomer/go-sarah/v3"
"golang.org/x/xerrors"
"io"
"time"
)

var (
// ErrEmptyPayload is an error that represents empty payload.
ErrEmptyPayload = xerrors.New("empty payload was given")
ErrEmptyPayload = errors.New("empty payload was given")
)

// MessageReceiver defines an interface that receives RoomMessage.
Expand Down

0 comments on commit 7e8a0c6

Please sign in to comment.