Code Explainers

Code explainers tagged #error-handling

go
package api
 
import (
	"encoding/json"

Building reusable JSON helpers in Go HTTP handlers

http json error-handling
Intermediate 9 steps
ruby
require "zlib"
require "stringio"
require "base64"
 

Compress-and-encode payloads in Ruby

compression encoding io-streams
Intermediate 8 steps
rust
use std::fs;
use std::path::Path;
 
use regex::Regex;

Redacting emails in a file with Rust

regex file-io closures
Intermediate 6 steps
rust
use std::fs::{File, OpenOptions};
use std::io::{self, Write};
use std::path::Path;
 

A buffered log writer in Rust

buffering raii file-io
Intermediate 9 steps
go
package token
 
import (
	"crypto/rand"

Generating secure random tokens in Go

cryptography randomness encoding
Intermediate 5 steps
rust
use axum::{
    extract::{FromRef, FromRequestParts},
    http::{request::Parts, StatusCode},
    response::{IntoResponse, Response},

A custom API-key extractor in Axum

authentication extractors trait-implementation
Advanced 8 steps
java
public OrderConfirmation submitOrder(Order order) throws IOException, InterruptedException {
    String payload = objectMapper.writeValueAsString(order);
 
    HttpRequest request = HttpRequest.newBuilder()

Posting an order with Java's HttpClient

http-client json-serialization error-handling
Intermediate 6 steps
go
func GetArticle(c *gin.Context) {
	id := c.Param("id")
 
	article, err := articleRepo.FindByID(c.Request.Context(), id)

Conditional GET with ETags in Gin

http-caching etag conditional-requests
Intermediate 7 steps
rust
use axum::{
    async_trait,
    extract::{FromRequestParts, State},
    http::{header, request::Parts, StatusCode},

Custom auth extractors in Axum

extractors authentication error-handling
Intermediate 9 steps
typescript
import { z, type ZodType } from "zod";
 
export class HttpError extends Error {
  constructor(

A type-safe fetch wrapper with Zod

generics runtime-validation error-handling
Intermediate 8 steps
rust
use std::fmt;
use std::str::FromStr;
 
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

A validated Email newtype in Rust

newtype validation error-handling
Intermediate 9 steps
go
func (r *EventRepository) BatchInsert(ctx context.Context, events []Event) error {
	if len(events) == 0 {
		return nil
	}

Batch inserting rows in a Go transaction

transactions batching database
Intermediate 7 steps
typescript
import { Controller, Get, Param, StreamableFile, Res, NotFoundException, Header } from '@nestjs/common';
import { Response } from 'express';
import { createReadStream, promises as fsp } from 'fs';
import { join } from 'path';

Streaming CSV file downloads in NestJS

file-streaming http-headers error-handling
Intermediate 7 steps
rust
pub struct ScopeGuard<F: FnMut()> {
    rollback: F,
    active: bool,
}

A RAII rollback guard in Rust

raii error-handling closures
Advanced 8 steps
ruby
class HealthController < ApplicationController
  skip_before_action :authenticate_user!, raise: false
  skip_forgery_protection
 

Building a health check endpoint in Rails

health-check monitoring error-handling
Intermediate 7 steps
rust
use axum::{extract::{Path, State}, http::StatusCode, Json};
use serde::Serialize;
use std::sync::Arc;
 

Concurrent dashboard aggregation in Axum

concurrency error-handling graceful-degradation
Intermediate 7 steps
javascript
const express = require('express');
const router = express.Router();
 
router.get('/articles/:slug', async (req, res, next) => {

Content negotiation with res.format in Express

content-negotiation routing error-handling
Intermediate 9 steps
rust
use axum::{
    extract::{Path, State},
    http::{header, HeaderMap, StatusCode},
    response::{IntoResponse, Response},

Optimistic concurrency with ETags in Axum

optimistic-concurrency etag http-headers
Intermediate 8 steps