go
18 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package domain
import (
"errors"
"fmt"
)
var (
ErrNotFound = errors.New("not found")
ErrConflict = errors.New("conflict")
)
func WrapNotFound(resource string, cause error) error {
if cause == nil {
return fmt.Errorf("%s: %w", resource, ErrNotFound)
}
return fmt.Errorf("%s: %w: %w", resource, ErrNotFound, cause)
}
1 file · go
Explain with highlit
In production, you want errors that are both human-readable and machine-checkable. I wrap errors with %w so callers can still match them using errors.Is and errors.As. This avoids string comparisons like if err.Error() == ..., which break on refactors. I also like defining sentinel errors for well-known conditions (ErrNotFound, ErrConflict) and returning them from lower layers with context using fmt.Errorf. The service layer can then map those to HTTP status codes without losing the original cause. The important discipline is consistency: don’t mix ad-hoc errors with sentinel errors for the same condition. If you follow that rule, you get clean logs and predictable error handling across the codebase.
Related snips
go
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
go
observability
build
by Leah Thompson
1 tab
ruby
payload = {
sub: user.id,
iss: 'https://auth.example.com',
aud: 'codesnips-api',
exp: 15.minutes.from_now.to_i,
iat: Time.now.to_i,
JWT issuance and verification without common footguns
jwt
authentication
api
by Kai Nakamura
2 tabs
ruby
module Api
module V1
class UsersController < BaseController
def show
user = User.includes(:profile).find(params[:id])
ETags for conditional requests and caching
rails
caching
http-caching
by Alex Kumar
1 tab
go
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
go
postgres
transactions
by Leah Thompson
1 tab
graphql
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
GraphQL API with Spring Boot
java
graphql
spring-boot
by David Kumar
3 tabs
typescript
import axios, { AxiosError } from 'axios'
import { v4 as uuidv4 } from 'uuid'
const api = axios.create({
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3000/api/v1',
timeout: 15000,
Axios API client with interceptors
react
axios
api
by Maya Patel
1 tab
Share this code
Here's the card — post it anywhere.