diff --git a/shorturl.go b/shorturl.go index 3e7a062..6c1a21b 100644 --- a/shorturl.go +++ b/shorturl.go @@ -3,15 +3,24 @@ package main import ( "bytes" "encoding/json" + "errors" "net/http" "github.com/zenazn/goji/web" ) -type shortenedURL struct { +type shortenerRequest struct { + LongURL string `json:"longUrl"` +} + +type shortenerResponse struct { Kind string `json:"kind"` ID string `json:"id"` LongURL string `json:"longUrl"` + Error struct { + Code int `json:"code"` + Message string `json:"message"` + } `json:"error"` } func shortURLHandler(c web.C, w http.ResponseWriter, r *http.Request) { @@ -25,14 +34,14 @@ func shortURLHandler(c web.C, w http.ResponseWriter, r *http.Request) { metadata, err := metadataRead(fileName) if err != nil { - oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.") + oopsHandler(c, w, r, RespJSON, "Corrupt metadata.") return } if metadata.ShortURL == "" { url, err := shortenURL(getSiteURL(r) + fileName) if err != nil { - oopsHandler(c, w, r, RespAUTO, "Something went wrong") + oopsHandler(c, w, r, RespJSON, err.Error()) return } @@ -40,7 +49,7 @@ func shortURLHandler(c web.C, w http.ResponseWriter, r *http.Request) { err = metadataWrite(fileName, &metadata) if err != nil { - oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.") + oopsHandler(c, w, r, RespJSON, "Corrupt metadata.") return } } @@ -58,7 +67,7 @@ func shortenURL(url string) (string, error) { apiURL += "?key=" + Config.googleShorterAPIKey } - jsonStr, _ := json.Marshal(shortenedURL{LongURL: url}) + jsonStr, _ := json.Marshal(shortenerRequest{LongURL: url}) req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonStr)) req.Header.Set("Content-Type", "application/json") @@ -70,11 +79,15 @@ func shortenURL(url string) (string, error) { } defer resp.Body.Close() - shortened := new(shortenedURL) - err = json.NewDecoder(resp.Body).Decode(shortened) + shortenerResponse := new(shortenerResponse) + err = json.NewDecoder(resp.Body).Decode(shortenerResponse) if err != nil { return "", err } - return shortened.ID, err + if shortenerResponse.Error.Message != "" { + return "", errors.New(shortenerResponse.Error.Message) + } + + return shortenerResponse.ID, nil } diff --git a/static/js/shorturl.js b/static/js/shorturl.js index 65d7e2d..c17154a 100644 --- a/static/js/shorturl.js +++ b/static/js/shorturl.js @@ -7,16 +7,18 @@ document.getElementById('shorturl').addEventListener('click', function (e) { xhr.open("GET", e.target.dataset.url, true); xhr.setRequestHeader('Accept', 'application/json'); xhr.onreadystatechange = function () { - if (xhr.readyState == 4 && xhr.status === 200) { - + if (xhr.readyState === 4) { var resp = JSON.parse(xhr.responseText); - if (!resp.shortUrl) return; - e.target.innerText = resp.shortUrl; - e.target.href = resp.shortUrl; - e.target.setAttribute('aria-label', 'Click to copy into clipboard') + if (xhr.status === 200 && resp.error == null) { + e.target.innerText = resp.shortUrl; + e.target.href = resp.shortUrl; + e.target.setAttribute('aria-label', 'Click to copy into clipboard') - copy(resp.shortUrl); + copy(resp.shortUrl); + } else { + e.target.setAttribute('aria-label', resp.error) + } } }; xhr.send();