error-handling

javascript
// Creating a Promise
const myPromise = new Promise((resolve, reject) => {
  const success = true;

  setTimeout(() => {
    if (success) {

Promises and async/await patterns for asynchronous JavaScript

javascript promises async-await
by Alex Chang 1 tab
ruby
class CreateDeadJobs < ActiveRecord::Migration[7.1]
  def change
    create_table :dead_jobs do |t|
      t.string  :jid, null: false
      t.string  :queue, null: false
      t.string  :klass, null: false

Background Job Dead Letter Queue (DLQ) Table

rails reliability background-jobs
by codesnips 4 tabs
typescript
import axios from 'axios';

export type NormalizedErrors = {
  fields: Record<string, string>;
  formLevel: string | null;
};

Frontend: normalize and display server validation errors

ux typescript react
by codesnips 3 tabs
rust
use anyhow::{Context, Result};
use std::fs;

fn load_config(path: &str) -> Result<String> {
    fs::read_to_string(path)
        .with_context(|| format!("failed to read config from {}", path))

anyhow::Context for adding error context without custom types

rust error-handling cli
by Marcus Chen 1 tab
ruby
module ApiErrorHandler
  extend ActiveSupport::Concern

  included do
    rescue_from StandardError, with: :handle_standard_error
    rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found

Structured JSON error responses

rails api error-handling
by Alex Kumar 1 tab
ruby
class CircuitBreaker
  class CircuitOpenError < StandardError; end

  INCREMENT = <<~LUA.freeze
    local n = redis.call('INCR', KEYS[1])
    if n == 1 then redis.call('EXPIRE', KEYS[1], ARGV[1]) end

Graceful Degradation: Feature-Based Rescue

rails resilience circuit-breaker
by codesnips 3 tabs
ruby
class Contract
  INVALID = Object.new.freeze

  COERCERS = {
    string: ->(v) { v.is_a?(String) ? v : v.to_s },
    integer: ->(v) { Integer(v.to_s) rescue INVALID },

Validated JSON Schema with dry-validation-style contract (lightweight)

api validation contracts
by codesnips 4 tabs
typescript
export class AppError extends Error {
  public readonly statusCode: number;
  public readonly code: string;
  public readonly isOperational: boolean;

  constructor(message: string, statusCode = 500, code = 'INTERNAL_ERROR', isOperational = true) {

Backend: normalize errors with a single Express handler

express error-handling typescript
by codesnips 4 tabs
rust
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum LogLevel {
    Info,
    Warn,

Streaming Log File Aggregation With Buffered Line Iteration in Rust

rust streaming file-io
by codesnips 3 tabs
python
from typing import Optional
from pydantic import BaseModel, EmailStr, Field, field_validator, model_validator


class CreateUserRequest(BaseModel):
    model_config = {"extra": "forbid"}

Validating JSON Payloads with Pydantic v2 and Returning Field-Level Errors in FastAPI

fastapi pydantic validation
by codesnips 3 tabs
java
package com.example.auth.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

Collect Bean Validation Field Errors in a Spring Boot Signup Endpoint

spring-boot bean-validation jakarta-validation
by codesnips 3 tabs
typescript
export type ErrorCode =
  | 'VALIDATION'
  | 'UNAUTHENTICATED'
  | 'FORBIDDEN'
  | 'NOT_FOUND'
  | 'CONFLICT'

API error shape that frontend can rely on

typescript express react
by codesnips 4 tabs