Structured Stream Errors

StreamErrorMode::Error|Exception|Silent; StreamException::getErrors(): array; StreamError->code: StreamErrorCode; StreamError->wrapperName: string; stream_last_errors(): array; stream_clear_errors(): void

The PHP 8.6 stream error family: the error_mode context option turns stream failures into thrown StreamExceptions or silence — errors become structured objects of StreamErrorCode enum + wrapperName + path, ending the era of the warning string.

C C23

C23
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main(void) {
    FILE *f = fopen("/definitely/not/here/a55.txt", "r");
    if (f == NULL) {
        /* errno is a global int, not an object: the message comes from
           strerror, and there is no code enum to switch on. */
        printf("code=ENOENT:%d\n", errno == ENOENT);
        printf("msg=%s\n", strerror(errno));
    }
    return 0;
}
Output
code=ENOENT:1
msg=No such file or directory

C++ C++26

C++26
#include <cstdio>
#include <system_error>

int main() {
    std::error_code ec; // structured: value + category, but still errno-shaped
    FILE *f = fopen("/definitely/not/here/a55.txt", "r");
    if (f == nullptr) {
        ec = std::error_code(errno, std::system_category());
        std::printf("kind=%d\n", ec == std::errc::no_such_file_or_directory);
        std::printf("value=%d\n", ec.value());
    } else {
        std::fclose(f);
    }
    return 0;
}
Output
kind=1
value=2

C# v11.0.100

v11.0.100
using System;
using System.IO;

try
{
    File.OpenRead("/definitely_not_here_a55.txt"); // no parent dirs missing
}
catch (FileNotFoundException e)
{
    Console.WriteLine(e.GetType().Name);
    Console.WriteLine(e.FileName.Contains("a55.txt"));
}
catch (IOException e)
{
    Console.WriteLine("io: " + e.GetType().Name);
}
Output
FileNotFoundException
True

Go v1.27.4

v1.27.4
package main

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
)

func main() {
	_, err := os.Open("/definitely/not/here/a55.txt")
	if err != nil {
		// every error is a value implementing error; errors.Is/As
		// destructure it without touching message strings.
		fmt.Println(errors.Is(err, fs.ErrNotExist))
		var perr *fs.PathError
		fmt.Println(errors.As(err, &perr))
		fmt.Println(perr.Op)
	}
}
Output
true
true
open

Java v27.0.1

v27.0.1
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) {
        try {
            Files.newInputStream(Path.of("/definitely/not/here/a55.txt"));
        } catch (NoSuchFileException e) {
            // typed exception: class name + path, no string parsing
            System.out.println(e.getClass().getSimpleName());
            System.out.println(e.getMessage().contains("a55.txt"));
        } catch (IOException e) {
            System.out.println("other: " + e.getClass().getSimpleName());
        }
    }
}
Output
NoSuchFileException
true

JavaScript v26.13.1

v26.13.1
const fs = require('node:fs');
try {
  fs.readFileSync('/definitely/not/here/a55.txt');
} catch (e) {
  console.log(e instanceof Error);
  console.log(e.code);   // structured code string, not a warning
  console.log(e.errno === -2); // libuv's ENOENT
  console.log(e.path === '/definitely/not/here/a55.txt');
}
Output
true
ENOENT
true
true

PHP v8.6.0

v8.6.0
// Exception mode: stream failures throw StreamException instead of warning
$ctx = stream_context_create([
    'stream' => ['error_mode' => StreamErrorMode::Exception],
]);
try {
    fopen('/definitely/not/here/a55.txt', 'r', false, $ctx);
    var_dump(false);
} catch (StreamException $e) {
    $errs = $e->getErrors();
    var_dump(count($errs) === 1);
    var_dump($errs[0]->code === StreamErrorCode::OpenFailed);
    var_dump($errs[0]->code->name);
    var_dump(is_string($errs[0]->message) && $errs[0]->message !== '');
    var_dump($errs[0]->terminating);
}

// Silent mode: false return + errors kept for stream_last_errors()
$ctx2 = stream_context_create([
    'stream' => ['error_mode' => StreamErrorMode::Silent],
]);
var_dump(fopen('/definitely/not/here/a55.txt', 'r', false, $ctx2) === false);
$stored = stream_last_errors();
var_dump(count($stored) === 1);
var_dump($stored[0]->code === StreamErrorCode::OpenFailed);
Output
bool(true)
bool(true)
string(10) "OpenFailed"
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

Python v3.15.2

v3.15.2
try:
    f = open("/definitely/not/here/a55.txt", "r")
except OSError as e:
    print(type(e).__name__)
    print(e.errno == 2)  # structured code, not a warning string
    print(e.strerror == "No such file or directory")
else:
    f.close()
Output
FileNotFoundError
True
True

Rust v1.99.1

v1.99.1
use std::io::ErrorKind;

fn main() {
    match std::fs::File::open("/definitely/not/here/a55.txt") {
        Err(e) => {
            println!("{}", e.kind() == ErrorKind::NotFound);
            println!("{}", e.raw_os_error() == Some(2)); // ENOENT
        }
        Ok(_) => println!("opened"),
    }
}
Output
true
true

TypeScript v26.13.1

v26.13.1
import * as fs from 'node:fs';
// typed errors carry code/errno/path as first-class fields
try {
  fs.readFileSync('/definitely/not/here/a55.txt');
} catch (e) {
  const err = e as NodeJS.ErrnoException;
  console.log(err instanceof Error);
  console.log(err.code);
  console.log(err.errno === -2);
  console.log(err.path === '/definitely/not/here/a55.txt');
}
Output
true
ENOENT
true
true

Language differences

C: the errno global int plus strerror lookup (example): the error is a process-wide global — no object, no enum, with per-thread errno as the multi-threading patch; the contrast core: C's "structure" is exactly one layer, an integer code.

C++: std::error_code (example): a value+category pair sitting halfway between errno and exceptions; std::errc provides the portable enum (no_such_file_or_directory at the same tier as OpenFailed), but there are still no stream-context fields like wrapperName/path.

C#: the FileNotFoundException/DirectoryNotFoundException/IOException family (example): the type is the classification (GetType().Name) and FileName carries the path — structurally on par with StreamError; the catch chain from specific to general is the very taxonomy the StreamErrorCode enum sets out to unify.

Go: error interface values (example): os.Open returns a *fs.PathError{Op, Path, Err} struct — Op (open) ↔ wrapperName, Path ↔ path, Err ↔ code; destructuring via errors.Is/As never touches message strings, the classic demonstration of structured vs string.

Java: typed exceptions like NoSuchFileException (example): FileSystemException carries file/other fields; graded catching from NoSuchFileException to IOException springs from the same taxonomy that StreamErrorCode's granularity encodes.

JavaScript: Error subclasses plus code/errno/path properties (example): the ENOENT string code plus libuv's negative errno; the ecosystem's heterogeneous errors (strings/Errors/undefined) mirror PHP's warning-string predicament, and the structured properties are the part Node got right from day one.

PHP: the 8.6 RFC stream_errors (example): the stream context's error_mode has three states — Error (default warnings, the old world since PHP 5.x), Exception (throws StreamException, getErrors() returning a list of StreamError), and Silent (returns false, errors kept for stream_last_errors(), cleared by stream_clear_errors()); the StreamError object carries code (the StreamErrorCode enum with dozens of cases — OpenFailed/ConnectFailed/TimeOut and more) + message + wrapperName + path/URL + severity + terminating; error_get_last (contrast) only offers the generic type/message/file/line quartet, with stream context recoverable only by regexing the string — this entry completes the "warning string → structured object" evolution.

Python: the OSError family (example): open raises FileNotFoundError (an OSError subclass) with the twin structured fields errno==2 (POSIX ENOENT) and strerror; the exception hierarchy (OSError → FileNotFoundError/PermissionError/…) and the StreamErrorCode enum are two implementations of one taxonomy.

Rust: io::Error (example): the cross-platform ErrorKind::NotFound enum plus raw_os_error() keeping the original code — the "portable enum + platform raw code" split mirrors StreamErrorCode + severity; Result forcing handling makes "missing a warning" impossible at the type level.

TypeScript: same as JS (example): the NodeJS.ErrnoException interface types code?: string, errno?: number, path?: string — a typed contract for structured fields that gives e.code !== 'ENOENT' a compile-time check.