Socket
Book a DemoInstallSign in
Socket

go.winto.dev/handler

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

go.winto.dev/handler

v1.0.2
Go
Version published
Created
Source

handler

GoDoc

Package handler provide new signature for handling http request.

stdlib Handler signature is func(http.ResponseWriter, *http.Request), and it is not convenience to write branching inside it.

For example:

func h(w http.ResponseWriter, r *http.Request) {
    if ... {
        http.Error(w, "some error 1", 500)
        // it will be disaster if we forget this return
        return
    }

    ...

    if ... {
        http.Error(w, "some error 2", 500)
        // it will be disaster if we forget this return
        return
    }

    ...

    fmt.Fprintln(w, "some data")
}

we can rewrite it like this to force developer to return when the branch of the code end:

func h(r *http.Request) http.HandlerFunc {
    if ... {
        return defresponse.Text(500, "some error 1")
    }

    ...


    if ... {
        return defresponse.Text(500, "some error 2")
    }

    ...

    // will compile error if we forget return
    return defresponse.Text(200, "some data")
}

FAQs

Package last updated on 21 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.