Code Explainers

Code explainers tagged #error-handling

php
<?php
 
function uploadDocument(string $endpoint, string $filePath, array $meta): array
{

Uploading a file with cURL in PHP

curl file-upload http
Intermediate 8 steps
typescript
type Event = Record<string, unknown>;
 
interface BatcherOptions {
  endpoint: string;

Batching analytics events in TypeScript

batching buffering async
Intermediate 8 steps
javascript
const logger = require('./logger');
 
class AppError extends Error {
  constructor(message, statusCode = 500, details = null) {

Centralized error handling in Express

error-handling middleware custom-errors
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

Resilient HTTP retries with Laravel's client

http-client retry-logic error-handling
Intermediate 7 steps
rust
use std::fs::File;
use std::io::{self, BufWriter};
use std::path::Path;
 

Serializing config to JSON in Rust

serialization serde file-io
Intermediate 7 steps
rust
use std::collections::HashMap;
use std::fmt;
 
#[derive(Debug)]

Parsing a config file with typed errors in Rust

error-handling enums parsing
Intermediate 9 steps
rust
use chrono::{DateTime, Duration, FixedOffset, Utc};
 
#[derive(Debug)]
pub struct EventWindow {

Parsing and measuring time windows in Rust

datetime error-handling pattern-matching
Intermediate 7 steps
rust
use axum::{extract::Multipart, http::StatusCode, Json};
use serde::Serialize;
use tokio::io::AsyncWriteExt;
use uuid::Uuid;

Streaming multipart uploads in Axum

multipart streaming async-io
Intermediate 10 steps
python
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
 

Parallel file downloads with a thread pool

concurrency thread-pool streaming-io
Intermediate 8 steps
go
package validate
 
import (
	"fmt"

Struct validation with reflection in Go

reflection struct-tags validation
Intermediate 7 steps
ruby
class ParamCoercer
  TYPES = {
    integer: ->(v) { Integer(v) },
    float:   ->(v) { Float(v) },

Coercing request params by schema in Ruby

lambdas type-coercion lookup-table
Intermediate 7 steps
rust
use std::env;
use std::time::Duration;
 
#[derive(Debug, Clone)]

Loading typed config from environment variables in Rust

configuration error-handling generics
Intermediate 7 steps
python
import shutil
import uuid
from pathlib import Path
 

Safe avatar uploads in FastAPI

file-upload validation streaming
Intermediate 8 steps
javascript
async function copyToClipboard(text) {
  if (navigator.clipboard && window.isSecureContext) {
    try {
      await navigator.clipboard.writeText(text);

Copying to the clipboard with a fallback

clipboard progressive-enhancement dom
Intermediate 8 steps
rust
use std::time::Duration;
use tokio::time::sleep;
 
#[derive(Debug)]

Async retry with exponential backoff in Rust

retry exponential-backoff async
Intermediate 8 steps
rust
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
 

Recursive file search by extension in Rust

recursion filesystem error-handling
Intermediate 8 steps
typescript
type Ok<T> = { ok: true; value: T };
type Err<E> = { ok: false; error: E };
export type Result<T, E> = Ok<T> | Err<E>;
 

A Result type for typed error handling

discriminated-union error-handling type-guards
Intermediate 8 steps
javascript
'use server'
 
import { z } from 'zod'
import { redirect } from 'next/navigation'

How a Next.js Server Action validates a form

server-actions form-validation zod
Intermediate 7 steps