Code Explainers

Code explainers tagged #error-handling

rust
use serde::Deserialize;
 
#[derive(Debug, Deserialize)]
#[serde(untagged)]

Parsing flexible JSON shapes with serde

deserialization enums json
Intermediate 6 steps
ruby
require "shellwords"
require "open3"
 
module Backup

Building safe shell commands in Ruby

shell-out subprocess command-injection
Intermediate 7 steps
rust
use chrono::{Duration, NaiveDate};
 
#[derive(Debug)]
pub struct DateRange {

Parsing and iterating date ranges in Rust

error-handling iterators parsing
Intermediate 7 steps
rust
use axum::{
    async_trait,
    extract::{rejection::JsonRejection, FromRequest, Request},
    http::StatusCode,

A validated JSON extractor in Axum

extractors validation error-handling
Intermediate 8 steps
go
func UpdateArticle(store *ArticleStore) gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.Param("id")
 

Optimistic concurrency with If-Match in Gin

optimistic-locking http-preconditions etag
Intermediate 9 steps
python
from enum import Enum
from typing import Annotated
 
from fastapi import FastAPI, Query, Request, status

Custom validation error responses in FastAPI

validation error-handling enums
Intermediate 8 steps
rust
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

Opaque pagination cursors in Rust

pagination serialization base64
Intermediate 8 steps
ruby
require "erb"
 
class TemplateRenderer
  class MissingVariableError < StandardError; end

A safe ERB template renderer in Ruby

metaprogramming method_missing binding
Intermediate 10 steps
java
public final class RetryExecutor {
 
    private final int maxAttempts;
    private final Duration baseDelay;

Retry with exponential backoff in Java

retry exponential-backoff jitter
Intermediate 7 steps
ruby
class ReverseGeocodeSignupJob < ApplicationJob
  queue_as :default
 
  retry_on Geocoder::OverQueryLimitError, wait: 5.minutes, attempts: 3

How Rails jobs handle reverse geocoding

background-jobs error-handling geocoding
Intermediate 7 steps
go
package api
 
import (
	"encoding/json"

Safely decoding JSON in a Go HTTP handler

http json-decoding input-validation
Intermediate 8 steps
ruby
class SyncInventoryJob < ApplicationJob
  queue_as :integrations
 
  retry_on Faraday::TimeoutError,

Resilient background jobs with retry_on in Rails

background-jobs error-handling retries
Intermediate 8 steps
rust
use std::sync::Mutex;
use std::time::{Duration, Instant};
 
#[derive(Debug, Clone, Copy, PartialEq)]

Building a circuit breaker in Rust

circuit-breaker state-machine concurrency
Advanced 8 steps
rust
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
 

Streaming lines from a gzip file in Rust

iterators io-streaming decompression
Intermediate 6 steps
python
import functools
import time
 
from flask import current_app

Retrying deadlocked DB writes in Flask

decorators retry exponential-backoff
Intermediate 6 steps
go
func HandleWebhook(store WebhookStore, dispatcher *EventDispatcher) gin.HandlerFunc {
	return func(c *gin.Context) {
		body, err := c.GetRawData()
		if err != nil {

Building a resilient webhook handler in Gin

webhooks dependency-injection signature-verification
Intermediate 9 steps
javascript
const { z, ZodError } = require('zod');
 
function validate(schemas) {
  return (req, res, next) => {

A reusable Zod validation middleware for Express

middleware validation schema
Intermediate 8 steps
rust
use std::collections::HashMap;
use std::sync::Arc;
 
use axum::body::Bytes;

A concurrent webhook dispatcher in Axum

trait-objects concurrency shared-state
Intermediate 9 steps