ogenerror

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 3 Imported by: 1

README

ogenerror

Extract error details from ogen-generated client errors.

Problem

When an ogen-generated client receives an unexpected HTTP status code, it returns an error like:

decode response: unexpected status code: 403

The response body (which often contains useful error details) is buried in the error and not easily accessible.

Solution

This package provides utilities to extract the status code and response body from ogen errors.

Installation

go get github.com/agentplexus/ogen-tools/ogenerror

Usage

Parse full error details
import "github.com/agentplexus/ogen-tools/ogenerror"

resp, err := client.SomeMethod(ctx, req)
if err != nil {
    if status := ogenerror.Parse(err); status != nil {
        fmt.Printf("Status: %d\n", status.StatusCode)
        fmt.Printf("Body: %s\n", status.Body)
    }
}
Check status code
if ogenerror.IsStatus(err, 403) {
    // Handle forbidden
}

if ogenerror.Is4xx(err) {
    // Handle client error
}

if ogenerror.Is5xx(err) {
    // Handle server error
}
Get just the status code
code := ogenerror.StatusCode(err)
if code == 429 {
    // Rate limited
}

API

Function Description
Parse(err) *UnexpectedStatus Extract status code and body
StatusCode(err) int Get just the status code (0 if not ogen error)
IsStatus(err, code) bool Check for specific status code
Is4xx(err) bool Check if 4xx client error
Is5xx(err) bool Check if 5xx server error

Example: API-specific error parsing

// In your API client package
func ParseAPIError(err error) *MyAPIError {
    status := ogenerror.Parse(err)
    if status == nil {
        return nil
    }

    apiErr := &MyAPIError{StatusCode: status.StatusCode}

    // Parse your API's specific error format
    var resp struct {
        Error struct {
            Message string `json:"message"`
            Code    string `json:"code"`
        } `json:"error"`
    }
    if json.Unmarshal(status.Body, &resp) == nil {
        apiErr.Message = resp.Error.Message
        apiErr.Code = resp.Error.Code
    }

    return apiErr
}

Documentation

Overview

Package ogenerror provides utilities for extracting error details from ogen-generated client errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is4xx

func Is4xx(err error) bool

Is4xx returns true if the error is a 4xx client error.

func Is5xx

func Is5xx(err error) bool

Is5xx returns true if the error is a 5xx server error.

func IsStatus

func IsStatus(err error, code int) bool

IsStatus returns true if the error is an ogen UnexpectedStatusCodeError with the given status code.

func StatusCode

func StatusCode(err error) int

StatusCode extracts just the status code from an ogen error. Returns 0 if the error is not an ogen UnexpectedStatusCodeError.

Types

type UnexpectedStatus

type UnexpectedStatus struct {
	StatusCode int
	Body       []byte
}

UnexpectedStatus contains the status code and response body from an ogen UnexpectedStatusCodeError.

func Parse

func Parse(err error) *UnexpectedStatus

Parse extracts status code and response body from an ogen error. Returns nil if the error is not an ogen UnexpectedStatusCodeError.

Usage:

resp, err := client.SomeMethod(ctx, req)
if err != nil {
    if status := ogenerror.Parse(err); status != nil {
        fmt.Printf("Status: %d, Body: %s\n", status.StatusCode, status.Body)
    }
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL