Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created April 23, 2015 09:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlseguin/3fbe1aa749b1e58c485e to your computer and use it in GitHub Desktop.
Save karlseguin/3fbe1aa749b1e58c485e to your computer and use it in GitHub Desktop.
func LoadArticle(res http.ResponseWriter, req *http.Request) {
if article, err := FindArticleById(...)
if err != nil {
handleError(res, err)
return
}
if article == nil {
handleNotFound(res, err)
}
}
func FindArticleById(id string) (*Article, error) {
conn, err := db.Open()
if err != nil {
return nil, err
}
var ...
err := conn.QueryRow("find_article_by_id", id).Scan(.....)
if err != nil {
return nil, err
}
return &Article{....}, nil
}
@egonelbre
Copy link

package article

var NotFound = errors.New("Article not found.")

func ShowArticle(w http.ResponseWriter, r *http.Request) {
    article, err := FindByID(r.FormValue("id"))
    switch {
    case err == NotFound:
        // show error page
        w.WriteHeader(http.StatusNotFound)
        fmt.Fprintf(w, "article wasn't found, maybe you should look at X instead")
        return
    case err != nil:
        log.Println(err.Error())
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    fmt.Fprintf(w, "%s", article)
}

func LoadByID(id string) (*Article, error) {
    conn, err := db.Open()
    if err != nil {
        return nil, err
    }

    var article Article
    err = conn.QueryRow("find_article_by_id", id).Scan(&article)
    if err != nil {
        // check here whether it actually is a not found error...
        return nil, NotFound
    }
    return &article, nil
}

@egonelbre
Copy link

With GeneralErrorHandling:

package article

var NotFound = errors.New("Not found.")

func ShowArticle(w http.ResponseWriter, r *http.Request) {  
    if article, err := FindByID(r.FormValue("id")); err == nil {
        GeneralErrorHandler(w, err)
        return
    }
    fmt.Fprintf(w, "%s", article)
}

func LoadByID(id string) (*Article, error) {
    conn, err := db.Open()
    if err != nil {
        return nil, err
    }

    var article Article
    err = conn.QueryRow("find_article_by_id", id).Scan(&article)
    if err != nil {
        // check here whether it actually is a not found error...
        return nil, NotFound
    }
    return &article, nil
}

With panicking, there should be some sort of recovery happening at the upper level...

package article

var NotFound = errors.New("Not found.")

func ShowArticle(w http.ResponseWriter, r *http.Request) {  
    article := FindByID(r.FormValue("id"))
    fmt.Fprintf(w, "%s", article)
}

func LoadByID(id string) *Article {
    conn, err := db.Open()
    if err != nil {
        panic(err)
    }

    var article Article
    err = conn.QueryRow("find_article_by_id", id).Scan(&article)
    if err != nil {
        // check here whether it actually is a not found error...
        panic(NotFound)
    }
    return &article
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment