Code Explainers

Code explainers tagged #error-handling

rust
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
 

Batch inserts with per-item status in Axum

batch-processing error-handling serde
Intermediate 8 steps
rust
use std::time::Duration;
 
#[derive(Debug, PartialEq)]
pub enum ParseDurationError {

Parsing duration strings safely in Rust

parsing error-handling checked-arithmetic
Intermediate 8 steps
javascript
const express = require('express');
const router = express.Router();
 
router.get('/articles/:slug', async (req, res, next) => {

Conditional GET caching in Express

http-caching conditional-get routing
Intermediate 8 steps
go
package handlers
 
import (
	"net/http"

Custom validators and binding in Gin

validation struct-tags error-handling
Intermediate 8 steps
ruby
class ImageNormalizer
  ORIENTATION_TRANSFORMS = {
    1 => ->(img) {},
    2 => ->(img) { img.flop },

Correcting EXIF orientation in Ruby

lookup-table lambdas image-processing
Intermediate 7 steps
go
package middleware
 
import (
	"net/http"

Wrapping Gin requests in a DB transaction

middleware transactions error-handling
Intermediate 8 steps
rust
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
 
#[derive(Debug)]

Parsing an HTTP Range header in Rust

http-range parsing file-io
Intermediate 10 steps
javascript
const express = require('express');
const router = express.Router();
const db = require('../db');
 

Building a paginated orders page in Express

pagination routing sql-queries
Intermediate 7 steps
ruby
class Document < ApplicationRecord
  class StaleObjectError < StandardError
    def initialize(id)
      super("Document ##{id} was modified by another process")

Optimistic locking with retries in Rails

optimistic-locking concurrency transactions
Advanced 8 steps
go
type CreateUserInput struct {
	Name     string `json:"name" binding:"required,min=2,max=64"`
	Email    string `json:"email" binding:"required,email"`
	Password string `json:"password" binding:"required,min=8"`

Turning Gin validation errors into JSON

validation request-binding error-handling
Intermediate 9 steps
javascript
const express = require('express');
const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware');
 
const router = express.Router();

Building an API gateway proxy in Express

reverse-proxy middleware api-gateway
Intermediate 6 steps
rust
use axum::{
    extract::{FromRequestParts, Query},
    http::{request::Parts, StatusCode},
};

Building a custom Axum extractor for query filters

extractors query-parsing enums
Intermediate 8 steps
python
import asyncio
from dataclasses import dataclass
 
import aiohttp

Bounded-concurrency HTTP fetching with asyncio

async concurrency semaphore
Intermediate 8 steps
go
package api
 
import (
	"errors"

Turning Gin validation errors into JSON

validation error-handling http-handlers
Intermediate 9 steps
python
from flask import Blueprint, jsonify
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
 

Building a health check endpoint in Flask

health-check blueprint error-handling
Intermediate 6 steps
rust
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    Number(f64),
    Plus,

How a tokenizer turns text into tokens

lexing enums iterators
Intermediate 8 steps
python
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

Redacting sensitive fields in FastAPI errors

validation error-handling security
Intermediate 7 steps
javascript
import { Component } from 'react';
import { reportError } from './services/telemetry';
 
export class ErrorBoundary extends Component {

How a React ErrorBoundary works

error-handling lifecycle-methods render-props
Intermediate 8 steps