<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: devalanx</title>
    <description>The latest articles on DEV Community by devalanx (@devalanx_5870ac2a80855272).</description>
    <link>https://dev.to/devalanx_5870ac2a80855272</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3445350%2Faa763401-945b-44e6-aec2-d291d5354cdc.png</url>
      <title>DEV Community: devalanx</title>
      <link>https://dev.to/devalanx_5870ac2a80855272</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devalanx_5870ac2a80855272"/>
    <language>en</language>
    <item>
      <title>Must Go: Cleaner Error Handling for Go HTTP Servers</title>
      <dc:creator>devalanx</dc:creator>
      <pubDate>Tue, 19 Aug 2025 13:32:15 +0000</pubDate>
      <link>https://dev.to/devalanx_5870ac2a80855272/must-go-cleaner-error-handling-for-go-http-servers-3gjf</link>
      <guid>https://dev.to/devalanx_5870ac2a80855272/must-go-cleaner-error-handling-for-go-http-servers-3gjf</guid>
      <description>&lt;p&gt;Hey folks 👋&lt;/p&gt;

&lt;p&gt;I’ve been working on a little package for Go that I think makes error handling in HTTP handlers a lot less painful. It’s called &lt;code&gt;must_go&lt;/code&gt;, and it brings the &lt;strong&gt;Go “must” pattern&lt;/strong&gt; to HTTP servers.&lt;/p&gt;

&lt;p&gt;The Problem&lt;/p&gt;

&lt;p&gt;If you’ve written any Go HTTP servers, you know the drill:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You end up repeating this pattern everywhere. It clutters up your code and makes handlers harder to read.&lt;/p&gt;

&lt;h2&gt;
  
  
  The “Must” Pattern in Go
&lt;/h2&gt;

&lt;p&gt;Go often uses the “must” pattern for functions where errors are unexpected or should crash fast. For example, &lt;code&gt;template.Must&lt;/code&gt; or &lt;code&gt;regexp.MustCompile&lt;/code&gt;. Instead of forcing you to handle errors at every call site, the “must” approach panics and lets the program crash (or recover in a controlled way).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;must_go&lt;/code&gt; extends this idea to HTTP servers. You can write your handlers using must-style error handling, and middleware will recover and return proper HTTP responses instead of killing your app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Must(err): Panic if there’s an error.&lt;/li&gt;
&lt;li&gt;MustWithMessage(err, msg): Add context.&lt;/li&gt;
&lt;li&gt;MustHTTP(err, status, msg): Panic with HTTP status + message.&lt;/li&gt;
&lt;li&gt;MustHTTPWithDefault(err): Automatically maps errors like “not found”, “unauthorized”, etc., to the right status codes.&lt;/li&gt;
&lt;li&gt;Helpers like MustNotFound, MustBadRequest, MustInternal, and more.&lt;/li&gt;
&lt;li&gt;Recovery middleware that catches panics and returns clean JSON:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "error": {
    "message": "User not found",
    "status": 404
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mux := http.NewServeMux()
mux.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
    err := fmt.Errorf("user not found")
    must_go.MustHTTP(err, http.StatusNotFound, "User not found")
})

handler := must_go.RecoveryMiddleware(mux)
http.ListenAndServe(":8080", handler)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No boilerplate, no repeated &lt;code&gt;if err != nil&lt;/code&gt; — just straight to the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use It?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Implements the familiar Go “must” pattern for HTTP servers&lt;/li&gt;
&lt;li&gt;Cleaner, more readable handlers&lt;/li&gt;
&lt;li&gt;Automatic, consistent error responses&lt;/li&gt;
&lt;li&gt;Keeps your business logic front and center&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Try It Out
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;go get github.com/Devalanx/must_go
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repo: &lt;a href="https://github.com/Devalanx/must_go" rel="noopener noreferrer"&gt;github.com/Devalanx/must_go&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’d love feedback! 🚀&lt;br&gt;
Give it a spin, open an issue, or suggest improvements.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
