<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Supriya Meshram on Medium]]></title>
        <description><![CDATA[Stories by Supriya Meshram on Medium]]></description>
        <link>https://medium.com/@supriyameshram171?source=rss-f0a8afc3467------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Supriya Meshram on Medium</title>
            <link>https://medium.com/@supriyameshram171?source=rss-f0a8afc3467------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 20:55:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@supriyameshram171/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Seven Years of Go Error Handling: The Mistakes I Made and What I Do Now]]></title>
            <link>https://medium.com/@supriyameshram171/seven-years-of-go-error-handling-the-mistakes-i-made-and-what-i-do-now-7f22f5cd82db?source=rss-f0a8afc3467------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f22f5cd82db</guid>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[programing]]></category>
            <category><![CDATA[golang-error-handling]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Supriya Meshram]]></dc:creator>
            <pubDate>Wed, 02 Dec 2026 12:30:29 GMT</pubDate>
            <atom:updated>2026-12-02T12:33:07.387Z</atom:updated>
            <content:encoded><![CDATA[<p>When I started writing Go, I hated <strong>if err != nil. </strong>I saw it as noise, a tax the language made me pay on every line.</p><p>Seven years later, I think the opposite. Error handling is the most honest part of a Go codebase. It shows what can fail and what the author planned to do about it. It also shows, very clearly, how much the author understood.</p><p>Here are the mistakes I made along the way, and what I do now.</p><p><strong>Mistake 1: Ignoring errors because “it won’t fail”</strong></p><p>My early code was full of this:</p><blockquote>```go</blockquote><blockquote>data, _ := json.Marshal(order)</blockquote><blockquote>```</blockquote><p>It works until the day <strong>order </strong>contains something that can’t be marshaled, and you send an empty payload downstream with no clue why.</p><p>The sneakier version is <strong>defer f.Close()</strong> on a file you wrote to. For writes, <strong>Close </strong>can return the error that tells you the data never reached disk. Ignoring it means silent data loss.</p><p><strong>What I do now:</strong> I don’t discard an error unless I can explain why in a comment. For writes, I check <strong>Close</strong> explicitly:</p><pre>if err := f.Close(); err != nil {<br>return fmt.Errorf(&quot;close %s: %w&quot;, path, err)<br>}</pre><p><strong>Mistake 2: Returning bare errors</strong></p><blockquote>```go</blockquote><blockquote>func LoadConfig(path string) (*Config, error) {</blockquote><blockquote>f, err := os.Open(path)</blockquote><blockquote>if err != nil {</blockquote><blockquote>return nil, err</blockquote><blockquote>}</blockquote><blockquote>// …</blockquote><blockquote>}</blockquote><blockquote>```</blockquote><p>Three layers up, the log says <strong>open config.yaml: no such file or directory.</strong> Which service? Which request? Which step of startup? I spent more hours than I’d like to admit grepping to answer those questions.</p><p><strong>What I do now:</strong> I add context at every layer, and I wrap with <strong>%w</strong> so the original error stays inspectable:</p><pre><br>return nil, fmt.Errorf(“load config %q: %w”, path, err)</pre><p>The resulting message reads like a stack trace written for humans: <strong>`start server: load config “prod.yaml”: open prod.yaml: no such file or directory`.</strong></p><p><strong>Mistake 3: Logging and returning the error</strong></p><p>This one felt responsible at the time:</p><blockquote>```go</blockquote><blockquote>if err != nil {</blockquote><blockquote>log.Printf(“failed to save user: %v”, err)</blockquote><blockquote>return err</blockquote><blockquote>}</blockquote><blockquote>```</blockquote><p>Then the caller logged it too. And its caller. One failure produced five log lines, and during an incident that noise buried the real signal.</p><p><strong>What I do now</strong>: I handle each error exactly once. Either I return it, adding context, or I handle it, by logging, retrying, or falling back. Never both. Logging happens at the boundary, usually the HTTP handler or the worker loop, where I have the full picture.</p><p><strong>Mistake 4: Matching errors by string, and wrapping with %v</strong></p><blockquote>```go</blockquote><blockquote>if strings.Contains(err.Error(), “not found”) {</blockquote><blockquote>// handle missing record</blockquote><blockquote>}</blockquote><blockquote>```</blockquote><p>I’m not proud of this one. It breaks the moment someone rewords a message. A related mistake was wrapping with `%v`:</p><blockquote>```go</blockquote><blockquote>return fmt.Errorf(“get user: %v”, err) // chain is lost</blockquote><blockquote>```</blockquote><p><strong>%v</strong> turns the error into plain text, so <strong>errors.Is</strong> and <strong>errors.As</strong> can never see the original.</p><p><strong>What I do now</strong>: I define sentinel errors or custom types and check them properly:</p><pre><br>var ErrNotFound = errors.New(“not found”)<br>if errors.Is(err, ErrNotFound) {<br>// handle missing record<br>}<br>var ve *ValidationError<br>if errors.As(err, &amp;ve) {<br>// use ve.Field<br>}</pre><p>And I use <strong>%w</strong> whenever I wrap.</p><p><strong>Mistake 5: The typed nil trap</strong></p><p>This one cost me an afternoon. Look at this code:</p><blockquote>```go</blockquote><blockquote>type ValidationError struct{ Field string }</blockquote><blockquote>func (e *ValidationError) Error() string { return “invalid “ + e.Field }</blockquote><blockquote>func validate(u User) error {</blockquote><blockquote>var err *ValidationError</blockquote><blockquote>if u.Name == “” {</blockquote><blockquote>err = &amp;ValidationError{Field: “name”}</blockquote><blockquote>}</blockquote><blockquote>return err</blockquote><blockquote>}</blockquote><blockquote>```</blockquote><p>When <strong>u.Name</strong> is valid, <strong>validate </strong>returns… a non-nil error. An interface holding a nil <strong>*ValidationError</strong> is not equal to <strong>nil</strong>, so <strong>if err != nil</strong> is true, and the caller treats a valid user as a failure.</p><p><strong>What I do now:</strong> Functions return the <strong>error </strong>interface, and on success they return a literal <strong>nil</strong>. I never return a concrete error pointer variable that might be nil.</p><pre>if u.Name == “” {<br>return &amp;ValidationError{Field: “name”}<br>}<br>return nil</pre><p><strong>Mistake 6: Leaking internals to clients</strong></p><blockquote>```go</blockquote><blockquote>http.Error(w, err.Error(), http.StatusInternalServerError)</blockquote><blockquote>```</blockquote><p>Convenient while developing. In production, it can leak table names, file paths, and upstream hostnames to anyone who triggers an error.</p><p><strong>What I do now</strong>: I translate errors at the API boundary. Internally the error is rich and detailed. Externally the client gets a stable, safe response.</p><pre>switch {<br>case errors.Is(err, ErrNotFound):<br>writeJSON(w, http.StatusNotFound, &quot;resource not found&quot;)<br>default:<br>log.Printf(&quot;request_id=%s err=%v&quot;, reqID, err)<br>writeJSON(w, http.StatusInternalServerError, &quot;internal error&quot;)<br>}</pre><p>The client gets a request ID they can send to support, and I get the full error in the logs.</p><p><strong>My rules today</strong></p><p><em>Never ignore an error silently. If I do, I leave a comment explaining why.</em></p><p><em>Wrap with context and </em><strong><em>%w</em></strong><em> as the error travels up.</em></p><p><em>Handle once. Return it or deal with it, never both.</em></p><p><em>Check with </em><strong><em>errors.Is</em></strong><em> and </em><strong><em>errors.As</em></strong><em>, never with strings.</em></p><p><em>Return a literal </em><strong><em>nil</em></strong><em> on success.</em></p><p><em>Translate at the boundary. Detailed inside, safe outside.</em></p><p><strong>The mindset shift</strong></p><p>The biggest change wasn’t technical. I stopped seeing errors as an annoyance to get past and started seeing them as part of my program’s design. Every <strong>if err != nil</strong> is a decision point: what does this failure mean, and who needs to know?</p><p>Go makes you answer that question on every call. That’s tedious on a bad day, but it’s why Go services tend to fail in predictable, debuggable ways.</p><p>What’s the worst Go error handling mistake you’ve made? Tell me in the comments. I’m sure I haven’t made them all yet</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f22f5cd82db" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>