Skip to content

Error Handling

Comprehensive error handling with codes and utilities.

Basic Error Handling

zig
const z = @import("zigantic");

// Check validation result
if (z.String(3, 50).init("Jo")) |name| {
    std.debug.print("Valid: {s}\n", .{name.get()});
} else |err| {
    std.debug.print("Error: {s}\n", .{z.errorMessage(err)});
    std.debug.print("Code: {s}\n", .{z.errorCode(err)});
}

Error Types

ErrorMessageCode
TooShortvalue is too shortE001
TooLongvalue is too longE002
TooSmallvalue is too smallE003
TooLargevalue is too largeE004
InvalidEmailmust be a valid emailE010
InvalidUrlmust be a valid URLE011
MissingFieldfield is requiredE020
TypeMismatchwrong typeE021
WeakPasswordpassword is too weak-
MustBeEvenmust be even-
MustBeOddmust be odd-
NotMultiplemust be multiple-
MustBeHttpsmust be HTTPS-
NotInAllowedValuesnot in allowed-

ErrorList

Collect multiple errors:

zig
var errors = z.errors.ErrorList.init(allocator);
defer errors.deinit();

// Add errors
try errors.add("name", error.TooShort, "too short", "Jo");
try errors.addWithPath("user", "email", error.InvalidEmail, "invalid", null);
try errors.addIndexed("tags", 2, error.TooLong, "too long", null);
try errors.addWithCode("field", error.TooShort, "msg", null, "E001");

// Check errors
errors.hasErrors()     // true
errors.count()         // number of errors
errors.first()         // first error or null
errors.last()          // last error or null
errors.containsField("name")  // true
errors.containsErrorType(error.TooShort) // true

// Format output
const text = try errors.formatAll(allocator);
const json = try errors.toJsonArray(allocator);

Limited Error Collection

zig
// Collect max 5 errors
var errors = z.errors.ErrorList.initWithMax(allocator, 5);
defer errors.deinit();

JSON Output

zig
const json = try errors.toJsonArray(allocator);
// [{"field":"name","message":"too short","value":"Jo"},...]

Error Codes

zig
const err = z.errors.ValidationError.TooShort;
z.errorMessage(err) // "value is too short"
z.errorCode(err)    // "E001"

JSON Parsing Errors

zig
const User = struct {
    name: z.String(3, 50),
    age: z.Int(i32, 18, 120),
};

var result = try z.fromJson(User, json, allocator);
defer result.deinit();

if (!result.isValid()) {
    for (result.error_list.errors.items) |err| {
        std.debug.print("[{s}] {s}: {s}\n", .{
            z.errorCode(err.error_type),
            err.field,
            err.message,
        });
    }
}

Merge Error Lists

zig
var errors1 = z.errors.ErrorList.init(allocator);
var errors2 = z.errors.ErrorList.init(allocator);
// ... add errors ...
try errors1.merge(errors2);

Validation Errors vs Library Bugs

Important Distinction

Validation errors are expected behavior when users provide invalid data. Handle them normally.

Library bugs are unexpected internal errors that might indicate a problem with zigantic itself.

Validation Errors (Expected)

zig
// This is normal - user provided invalid data
if (z.String(3, 50).init("Jo")) |name| {
    std.debug.print("Valid: {s}\n", .{name.get()});
} else |err| {
    // Handle normally - this is NOT a library bug
    std.debug.print("Error: {s}\n", .{z.errorMessage(err)});
}

Library Bugs (Unexpected)

Only use reportInternalError for unexpected situations that might be library bugs:

zig
// Only for unexpected internal errors
z.reportInternalError("Unexpected null during parsing");

This will print a message with the GitHub issues URL for reporting.

Released under the MIT License.