Unmarshal SlackError as an error
It looks like Slack changed how they return an error from a string to a
hash.
This meant that we could just unmarshal it straight into a struct. With
a little helper function we automatically get go errors.
Tarrant
9 years ago
0 | 0 |
package slack
|
1 | 1 |
|
2 | 2 |
import (
|
3 | |
"errors"
|
4 | 3 |
"net/url"
|
5 | 4 |
"strconv"
|
6 | 5 |
)
|
|
57 | 56 |
return nil, err
|
58 | 57 |
}
|
59 | 58 |
if !response.Ok {
|
60 | |
return nil, errors.New(response.Error)
|
|
59 |
return nil, response.Error
|
61 | 60 |
}
|
62 | 61 |
return response, nil
|
63 | 62 |
}
|
1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"encoding/json"
|
4 | |
"errors"
|
5 | 4 |
"net/url"
|
6 | 5 |
"strings"
|
7 | 6 |
)
|
|
85 | 84 |
return nil, err
|
86 | 85 |
}
|
87 | 86 |
if !response.Ok {
|
88 | |
return nil, errors.New(response.Error)
|
|
87 |
return nil, response.Error
|
89 | 88 |
}
|
90 | 89 |
return response, nil
|
91 | 90 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | 2 |
import (
|
3 | |
"errors"
|
4 | 3 |
"net/url"
|
5 | 4 |
"strconv"
|
6 | 5 |
)
|
|
35 | 34 |
return nil, err
|
36 | 35 |
}
|
37 | 36 |
if !response.Ok {
|
38 | |
return nil, errors.New(response.Error)
|
|
37 |
return nil, response.Error
|
39 | 38 |
}
|
40 | 39 |
return response, nil
|
41 | 40 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | |
import (
|
3 | |
"errors"
|
4 | |
"net/url"
|
5 | |
)
|
|
2 |
import "net/url"
|
6 | 3 |
|
7 | 4 |
type emojiResponseFull struct {
|
8 | 5 |
Emoji map[string]string `json:"emoji"`
|
|
20 | 17 |
return nil, err
|
21 | 18 |
}
|
22 | 19 |
if !response.Ok {
|
23 | |
return nil, errors.New(response.Error)
|
|
20 |
return nil, response.Error
|
24 | 21 |
}
|
25 | 22 |
return response.Emoji, nil
|
26 | 23 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | 2 |
import (
|
3 | |
"errors"
|
4 | 3 |
"net/url"
|
5 | 4 |
"strconv"
|
6 | 5 |
)
|
|
49 | 48 |
return nil, err
|
50 | 49 |
}
|
51 | 50 |
if !response.Ok {
|
52 | |
return nil, errors.New(response.Error)
|
|
51 |
return nil, response.Error
|
53 | 52 |
}
|
54 | 53 |
return response, nil
|
55 | 54 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | |
import (
|
3 | |
"errors"
|
4 | |
"net/url"
|
5 | |
)
|
|
2 |
import "net/url"
|
6 | 3 |
|
7 | 4 |
type oAuthResponseFull struct {
|
8 | 5 |
AccessToken string `json:"access_token"`
|
|
24 | 21 |
return "", "", err
|
25 | 22 |
}
|
26 | 23 |
if !response.Ok {
|
27 | |
return "", "", errors.New(response.Error)
|
|
24 |
return "", "", response.Error
|
28 | 25 |
}
|
29 | 26 |
return response.AccessToken, response.Scope, nil
|
30 | 27 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | |
import (
|
3 | |
"errors"
|
4 | |
"net/url"
|
5 | |
)
|
|
2 |
import "net/url"
|
6 | 3 |
|
7 | 4 |
/*
|
8 | 5 |
Added as a var so that we can change this for testing purposes
|
|
21 | 18 |
}
|
22 | 19 |
|
23 | 20 |
type SlackResponse struct {
|
24 | |
Ok bool `json:"ok"`
|
25 | |
Error string `json:"error"`
|
|
21 |
Ok bool `json:"ok"`
|
|
22 |
Error *SlackError `json:"error"`
|
|
23 |
}
|
|
24 |
|
|
25 |
type SlackError struct {
|
|
26 |
Code int
|
|
27 |
Msg string
|
|
28 |
}
|
|
29 |
|
|
30 |
func (s SlackError) Error() string {
|
|
31 |
return s.Msg
|
26 | 32 |
}
|
27 | 33 |
|
28 | 34 |
type AuthTestResponse struct {
|
|
62 | 68 |
return nil, err
|
63 | 69 |
}
|
64 | 70 |
if !responseFull.Ok {
|
65 | |
return nil, errors.New(responseFull.Error)
|
|
71 |
return nil, responseFull.Error
|
66 | 72 |
}
|
67 | 73 |
return &responseFull.AuthTestResponse, nil
|
68 | 74 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | 2 |
import (
|
3 | |
"errors"
|
4 | 3 |
"net/url"
|
5 | 4 |
"strconv"
|
6 | 5 |
)
|
|
69 | 68 |
return nil, nil, err
|
70 | 69 |
}
|
71 | 70 |
if !response.Ok {
|
72 | |
return nil, nil, errors.New(response.Error)
|
|
71 |
return nil, nil, response.Error
|
73 | 72 |
}
|
74 | 73 |
return response.Items, &response.Paging, nil
|
75 | 74 |
}
|
0 | 0 |
package slack
|
1 | 1 |
|
2 | |
import (
|
3 | |
"errors"
|
4 | |
"net/url"
|
5 | |
)
|
|
2 |
import "net/url"
|
6 | 3 |
|
7 | 4 |
// UserProfile contains all the information details of a given user
|
8 | 5 |
type UserProfile struct {
|
|
63 | 60 |
return nil, err
|
64 | 61 |
}
|
65 | 62 |
if !response.Ok {
|
66 | |
return nil, errors.New(response.Error)
|
|
63 |
return nil, response.Error
|
67 | 64 |
}
|
68 | 65 |
return response, nil
|
69 | 66 |
}
|
1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"encoding/json"
|
4 | |
"errors"
|
5 | 4 |
"fmt"
|
6 | 5 |
"io"
|
7 | 6 |
"log"
|
|
46 | 45 |
return nil, err
|
47 | 46 |
}
|
48 | 47 |
if !response.Ok {
|
49 | |
return nil, errors.New(response.Error)
|
|
48 |
return nil, response.Error
|
50 | 49 |
}
|
51 | 50 |
api.info = response.Info
|
52 | 51 |
// websocket.Dial does not accept url without the port (yet)
|