Browse Source

fixed and improved error handling

pull/85/head
Atrox 10 years ago
parent
commit
da61072c99
  1. 29
      shorturl.go
  2. 8
      static/js/shorturl.js

29
shorturl.go

@ -3,15 +3,24 @@ package main
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"net/http" "net/http"
"github.com/zenazn/goji/web" "github.com/zenazn/goji/web"
) )
type shortenedURL struct {
type shortenerRequest struct {
LongURL string `json:"longUrl"`
}
type shortenerResponse struct {
Kind string `json:"kind"` Kind string `json:"kind"`
ID string `json:"id"` ID string `json:"id"`
LongURL string `json:"longUrl"` 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) { 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) metadata, err := metadataRead(fileName)
if err != nil { if err != nil {
oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
oopsHandler(c, w, r, RespJSON, "Corrupt metadata.")
return return
} }
if metadata.ShortURL == "" { if metadata.ShortURL == "" {
url, err := shortenURL(getSiteURL(r) + fileName) url, err := shortenURL(getSiteURL(r) + fileName)
if err != nil { if err != nil {
oopsHandler(c, w, r, RespAUTO, "Something went wrong")
oopsHandler(c, w, r, RespJSON, err.Error())
return return
} }
@ -40,7 +49,7 @@ func shortURLHandler(c web.C, w http.ResponseWriter, r *http.Request) {
err = metadataWrite(fileName, &metadata) err = metadataWrite(fileName, &metadata)
if err != nil { if err != nil {
oopsHandler(c, w, r, RespAUTO, "Corrupt metadata.")
oopsHandler(c, w, r, RespJSON, "Corrupt metadata.")
return return
} }
} }
@ -58,7 +67,7 @@ func shortenURL(url string) (string, error) {
apiURL += "?key=" + Config.googleShorterAPIKey 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, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -70,11 +79,15 @@ func shortenURL(url string) (string, error) {
} }
defer resp.Body.Close() 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 { if err != nil {
return "", err return "", err
} }
return shortened.ID, err
if shortenerResponse.Error.Message != "" {
return "", errors.New(shortenerResponse.Error.Message)
}
return shortenerResponse.ID, nil
} }

8
static/js/shorturl.js

@ -7,16 +7,18 @@ document.getElementById('shorturl').addEventListener('click', function (e) {
xhr.open("GET", e.target.dataset.url, true); xhr.open("GET", e.target.dataset.url, true);
xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Accept', 'application/json');
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status === 200) {
if (xhr.readyState === 4) {
var resp = JSON.parse(xhr.responseText); var resp = JSON.parse(xhr.responseText);
if (!resp.shortUrl) return;
if (xhr.status === 200 && resp.error == null) {
e.target.innerText = resp.shortUrl; e.target.innerText = resp.shortUrl;
e.target.href = resp.shortUrl; e.target.href = resp.shortUrl;
e.target.setAttribute('aria-label', 'Click to copy into clipboard') 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(); xhr.send();

Loading…
Cancel
Save