// GENERATED by tools/gen — DO NOT EDIT.

///|
/// A message reported by a structured API error envelope.
#derive(@debug.Debug)
pub(all) struct EnvelopeMessage {
  code : Int32
  message : String
  documentation_url : String?
  source_pointer : String?
}

///|
pub extend EnvelopeMessage with @debug.Debug::{to_repr}

///|
/// Response failure with the original head, bounded body and any decoded messages.
#derive(@debug.Debug)
pub(all) struct HttpFailure {
  operation : String
  head : @http.ResponseHead
  body : Bytes
  errors : Array[EnvelopeMessage]
  messages : Array[EnvelopeMessage]
}

///|
pub extend HttpFailure with @debug.Debug::{to_repr}

///|
/// Transport failures retain their kind and phase. Cancellation propagates.
#derive(@debug.Debug)
#non_exhaustive
pub(all) enum Failure {
  Http(HttpFailure)
  Transport(@http.TransportError)
  Decode(@client.ResponseFailure)
  Validation(operation~ : String, parameter~ : String, message~ : String)
}

///|
pub extend Failure with @debug.Debug::{to_repr}

///|
fn envelope_json(body : Bytes) -> Result[Json, String] {
  Result::Ok(@json.parse(@utf8.decode(body))) catch {
    error => Result::Err(error.to_string())
  }
}

///|
fn envelope_messages(
  value : Result[Json, String],
  field : String,
) -> Array[EnvelopeMessage] {
  guard value is Result::Ok(@json.Json::Object(fields)) &&
    fields.get(field) is Option::Some(@json.Json::Array(entries)) else {
    return []
  }
  entries.filter_map(entry => {
    guard entry is { "code": code, "message": @json.Json::String(message), .. } else {
      return Option::None
    }
    let code : Int32 = @json.from_json(code) catch { _ => return Option::None }
    let documentation_url = if entry
      is { "documentation_url": @json.Json::String(url), .. } {
      Option::Some(url)
    } else {
      Option::None
    }
    let source_pointer = if entry
      is { "source": { "pointer": @json.Json::String(pointer), .. }, .. } {
      Option::Some(pointer)
    } else {
      Option::None
    }
    Option::Some({ code, message, documentation_url, source_pointer, })
  })
}

///|
fn envelope_failure(
  operation : String,
  head : @http.ResponseHead,
  body : Bytes,
  value : Result[Json, String],
) -> HttpFailure {
  {
    operation,
    head,
    body: body[:body.length().min(@client.max_error_body)].to_owned(),
    errors: envelope_messages(value, "errors"),
    messages: envelope_messages(value, "messages"),
  }
}