vectordotdev/vector · error

Could not convert HTTP status code to JSON

Error message

Could not convert HTTP status code to JSON

What it means

vector-config implements `ToValue for http::StatusCode` by serializing `self.as_u16()` to JSON, panicking with "Could not convert HTTP status code to JSON" if serde_json rejects it. A u16 is always JSON-serializable in principle, so a panic here means serde_json itself refused a valid number (practically impossible, indicating a corrupted serde_json state or an unexpected serialization hook). It is a defensive invariant panic in schema generation.

Solutions

  1. Verify serde_json features are consistent across the workspace (arbitrary_precision can change number serialization behavior)
  2. Rebuild with a single unified serde_json version to avoid feature unification surprises
  3. If patching, serialize with serde_json::json!(self.as_u16()) which cannot fail for u16

Example fix

// before
serde_json::to_value(self.as_u16()).expect("Could not convert HTTP status code to JSON")
// after
serde_json::Value::Number(serde_json::Number::from(self.as_u16()))
Defensive patterns

Strategy: validation

Validate before calling

// u16 is always JSON-safe; validate the source value instead:
let code: u16 = status_code.as_u16();
debug_assert!(serde_json::to_value(code).is_ok());

Type guard

fn is_serializable_status(s: &http::StatusCode) -> bool {
    serde_json::to_value(s.as_u16()).is_ok()
}

Try / catch

// bypass the panicking impl entirely:
let v = serde_json::json!(status_code.as_u16());

Prevention

When it happens

Trigger: Calling `StatusCode::to_value()` during config schema generation when serde_json::to_value returns Err for a u16 — an effectively unreachable path kept as a guard against serde_json misconfiguration (e.g. arbitrary_precision mode incompatibilities in some versions).

Common situations: Generating schemas for HTTP-related component configs that include StatusCode fields; encountering this usually points to a serde_json feature mismatch in the build (e.g. conflicting arbitrary_precision settings).

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of vectordotdev/vector@bdb87aeaa4 (2026-09-16). Data as JSON: /api/errors/d0488d22dd743d06. Report an issue: GitHub.

Appendix: source

Thrown at lib/vector-config/src/http.rs:15

use std::cell::RefCell;

use http::StatusCode;
use serde_json::Value;
use vector_config_common::{attributes::CustomAttribute, constants};

use crate::{
    Configurable, GenerateError, Metadata, ToValue,
    num::NumberClass,
    schema::{SchemaGenerator, SchemaObject, generate_number_schema},
};

impl ToValue for StatusCode {
    fn to_value(&self) -> Value {
        serde_json::to_value(self.as_u16()).expect("Could not convert HTTP status code to JSON")
    }
}

impl Configurable for StatusCode {
    fn referenceable_name() -> Option<&'static str> {
        Some("http::StatusCode")
    }

    fn is_optional() -> bool {
        true
    }

    fn metadata() -> Metadata {
        let mut metadata = Metadata::default();
        metadata.set_description("HTTP response status code");
        metadata.add_custom_attribute(CustomAttribute::kv(
            constants::DOCS_META_NUMERIC_TYPE,
            NumberClass::Unsigned,

View on GitHub (pinned to bdb87aeaa4)