Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Assignment 1
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Aksel Baardsen
Assignment 1
Commits
d057bf9a
Commit
d057bf9a
authored
Oct 20, 2019
by
Aksel Baardsen
Browse files
Options
Downloads
Patches
Plain Diff
added better errorhandling (replaced bad switch-statement)
parent
bff44291
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/funcs.go
+10
-10
10 additions, 10 deletions
pkg/funcs.go
with
10 additions
and
10 deletions
pkg/funcs.go
+
10
−
10
View file @
d057bf9a
...
@@ -10,7 +10,7 @@ import (
...
@@ -10,7 +10,7 @@ import (
// creates http client for ensuring timely timeouts
// creates http client for ensuring timely timeouts
var
client
=
http
.
Client
{
var
client
=
http
.
Client
{
Timeout
:
1
5
*
time
.
Second
,
Timeout
:
1
0
*
time
.
Second
,
}
}
// allowing access to "overloaded" functions
// allowing access to "overloaded" functions
...
@@ -58,7 +58,7 @@ func getBody(url string, m mashup) error {
...
@@ -58,7 +58,7 @@ func getBody(url string, m mashup) error {
// sends appropriate error
// sends appropriate error
if
resp
.
StatusCode
!=
http
.
StatusOK
{
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
fmt
.
Errorf
(
"%d"
,
resp
.
StatusCode
)
return
fmt
.
Errorf
(
"
bad status code:
%d"
,
resp
.
StatusCode
)
}
}
// all went well
// all went well
...
@@ -70,17 +70,17 @@ func HttpError(w http.ResponseWriter, err error) {
...
@@ -70,17 +70,17 @@ func HttpError(w http.ResponseWriter, err error) {
// if timeout happened
// if timeout happened
if
strings
.
Contains
(
err
.
Error
(),
"Client.Timeout exceeded"
)
{
if
strings
.
Contains
(
err
.
Error
(),
"Client.Timeout exceeded"
)
{
http
.
Error
(
w
,
"Request timeout"
,
http
.
StatusGatewayTimeout
)
http
.
Error
(
w
,
"Request
to API
time
d
out"
,
http
.
StatusGatewayTimeout
)
return
// return if error is already displayed
return
// return if error is already displayed
}
}
switch
err
.
Error
()
{
// if a custom error was sent (caused by api response code)
case
"400"
:
if
strings
.
Contains
(
err
.
Error
(),
"bad status code:"
)
{
http
.
Error
(
w
,
"Bad Request: "
+
err
.
Error
(),
http
.
StatusBadRequest
)
http
.
Error
(
w
,
"Request to API timed out"
,
http
.
StatusGatewayTimeout
)
case
"404"
:
return
// return if error is already displayed
http
.
Error
(
w
,
"Not found: "
+
err
.
Error
(),
http
.
StatusNotFound
)
default
:
http
.
Error
(
w
,
"Error occurred"
,
http
.
StatusInternalServerError
)
}
}
// Default response in case of decoding errors or other errors
http
.
Error
(
w
,
"Internal server error"
,
http
.
StatusInternalServerError
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment