Skip to content

Commit 3070437

Browse files
committed
update error handling
1 parent 1fa9f8f commit 3070437

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

main.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,6 @@ func authHandler(req events.Request) (events.Response, error) {
128128

129129
func callbackHandler(req events.Request) (events.Response, error) {
130130
// TODO: Break this method apart
131-
errorRedirect, _ := events.Redirect("https://"+req.Headers["Host"], 303)
132-
133131
sess, err := sm.Read(req)
134132
if err != nil {
135133
log.Printf("Failed loading session cookie: %s", err)
@@ -140,34 +138,34 @@ func callbackHandler(req events.Request) (events.Response, error) {
140138

141139
if sess.Nonce == "" {
142140
log.Print("callback hit with no nonce")
143-
return errorRedirect, nil
141+
return events.Redirect("https://"+req.Headers["Host"], 303)
144142
} else if sess.Nonce != actual {
145143
log.Print("nonce mismatch; possible csrf OR cookies not enabled")
146-
return errorRedirect, nil
144+
return events.Fail("error; aborting")
147145
}
148146

149147
code := req.QueryStringParameters["code"]
150148
token, err := oauthCfg.Exchange(oauth2.NoContext, code)
151149
if err != nil {
152150
log.Print("there was an issue getting your token")
153-
return errorRedirect, nil
151+
return events.Fail("error; aborting")
154152
}
155153

156154
if !token.Valid() {
157155
log.Print("retreived invalid token")
158-
return errorRedirect, nil
156+
return events.Fail("error; aborting")
159157
}
160158

161159
client := github.NewClient(oauthCfg.Client(oauth2.NoContext, token))
162160
user, _, err := client.Users.Get(context.Background(), "")
163161
if err != nil {
164162
log.Print("error getting name")
165-
return errorRedirect, nil
163+
return events.Fail("error; aborting")
166164
}
167165
orgs, _, err := client.Organizations.List(context.Background(), "", &github.ListOptions{})
168166
if err != nil {
169167
log.Print("error getting orgs")
170-
return errorRedirect, nil
168+
return events.Fail("error; aborting")
171169
}
172170
var orgList []string
173171
for _, i := range orgs {
@@ -181,7 +179,7 @@ func callbackHandler(req events.Request) (events.Response, error) {
181179
cookie, err := sm.Write(sess)
182180
if err != nil {
183181
log.Print("error encoding cookie")
184-
return errorRedirect, nil
182+
return events.Fail("error; aborting")
185183
}
186184
return events.Response{
187185
StatusCode: 303,
@@ -196,39 +194,39 @@ func indexHandler(req events.Request) (events.Response, error) {
196194
// TODO: use nicer homepage template
197195
// TODO: Show if you're already auth'd
198196
// TODO: Show link to auth page
199-
//return events.Succeed("Index!")
200197
return events.Succeed(fmt.Sprintf("%+v\n", req))
201198
}
202199

203200
func defaultHandler(req events.Request) (events.Response, error) {
204201
return events.Redirect("https://"+req.Headers["Host"], 303)
205202
}
206203

207-
func loadConfig() {
204+
func loadConfig() (*configFile, error) {
205+
c := configFile{}
206+
208207
bucket := os.Getenv("S3_BUCKET")
209208
path := os.Getenv("S3_KEY")
210209
if bucket == "" || path == "" {
211-
log.Print("variables not provided")
212-
return
210+
return &c, fmt.Errorf("variables not provided")
213211
}
214212

215213
obj, err := s3.GetObject(bucket, path)
216214
if err != nil {
217-
log.Print(err)
218-
return
215+
return &c, err
219216
}
220217

221-
c := configFile{}
222218
err = yaml.Unmarshal(obj, &c)
219+
return &c, err
220+
}
221+
222+
func main() {
223+
var err error
224+
225+
config, err = loadConfig()
223226
if err != nil {
224227
log.Print(err)
225228
return
226229
}
227-
config = &c
228-
}
229-
230-
func main() {
231-
loadConfig()
232230

233231
sm = &sessionManager{
234232
Name: "session",

0 commit comments

Comments
 (0)