Code Explainers

Code explainers tagged #error-handling

python
from pathlib import Path
 
from PIL import Image, ImageOps
 

Batch image resizing with Pillow

image-processing batch-jobs error-handling
Intermediate 8 steps
go
package handler
 
type LineItem struct {
	SKU      string  `json:"sku" binding:"required,alphanum"`

Structured validation errors in Gin

validation json-binding error-handling
Intermediate 8 steps
javascript
import { useState, useCallback, useRef } from 'react';
 
function LikeButton({ postId, initialLiked, initialCount }) {
  const [liked, setLiked] = useState(initialLiked);

Optimistic like button with React

optimistic-ui abortcontroller error-handling
Advanced 7 steps
php
public function transaction(callable $callback, int $maxAttempts = 3): mixed
{
    $attempt = 0;
 

Retrying database deadlocks in PHP

retry transactions deadlock
Intermediate 8 steps
rust
use std::time::Duration;
 
#[derive(Debug, Clone)]
pub struct ServerConfig {

The builder pattern in Rust

builder-pattern ownership error-handling
Intermediate 8 steps
go
func ProxyDownload(c *gin.Context) {
	objectKey := c.Param("key")
	upstream := fmt.Sprintf("%s/files/%s", storageBaseURL, objectKey)
 

Streaming a proxied download in Gin

reverse-proxy streaming http-headers
Intermediate 8 steps
rust
use std::time::Duration;
 
use axum::{
    extract::State,

Rate-limit errors as Axum responses

error-handling rate-limiting http-headers
Intermediate 6 steps
java
package com.example.payments.config;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

Configuring a Stripe RestClient in Spring

dependency-injection http-client configuration
Intermediate 7 steps
go
package parser
 
import (
	"fmt"

Parsing access logs with named regex groups in Go

regex named-groups parsing
Intermediate 5 steps
java
public class GitHubUserClient {
 
    private final HttpClient http = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(10))

Calling the GitHub API with Java's HttpClient

http-client json-deserialization error-handling
Intermediate 9 steps
ruby
require "yaml"
require "pathname"
 
class AppConfig

A typed config loader in Ruby

yaml configuration recursion
Intermediate 8 steps
rust
use axum::{
    async_trait,
    body::Bytes,
    extract::FromRequest,

A strict JSON extractor in Axum

extractors traits content-negotiation
Advanced 7 steps
go
package archive
 
import (
	"bytes"

Gzip round-trip compression in Go

compression io error-handling
Intermediate 5 steps
go
package handlers
 
import (
	"encoding/json"

Hardening JSON decoding in Go HTTP handlers

json error-handling http
Intermediate 8 steps
ruby
class OrdersController < ApplicationController
  rescue_from ActiveRecord::RecordNotUnique, with: :handle_duplicate_submission
 
  def create

Idempotent order creation in Rails

idempotency database-constraints error-handling
Intermediate 8 steps
rust
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
 
#[derive(Debug)]

Parsing ps output into structs in Rust

subprocess parsing iterators
Intermediate 8 steps
ruby
class InventoryItem < ApplicationRecord
  belongs_to :warehouse
 
  validates :quantity, numericality: { greater_than_or_equal_to: 0 }

Safe inventory updates in Rails

concurrency pessimistic-locking optimistic-locking
Advanced 7 steps
typescript
import { HttpService } from '@nestjs/axios';
import { Injectable, Logger, ServiceUnavailableException } from '@nestjs/common';
import { AxiosError } from 'axios';
import { catchError, firstValueFrom, retry, timer } from 'rxjs';

Resilient payment retries in NestJS

retry exponential-backoff rxjs
Advanced 8 steps