///|
/// A fault in the hub protocol: a message that is not valid, or a message that
/// breaks the invocation rules. An endpoint that gets one closes the connection
/// with a Close message that names it.
#derive(Debug)
pub suberror ProtocolError {
  /// The text is not valid JSON.
  InvalidJson(detail~ : String)
  /// The JSON value is not an object.
  NotAnObject
  /// A required field is missing.
  MissingField(field~ : String)
  /// A field has a JSON type that the protocol does not permit for it.
  WrongFieldType(field~ : String, expected~ : String)
  /// The message has a field that its type does not define.
  UnknownField(field~ : String)
  /// The message type is not 1 to 7.
  UnsupportedMessageType(message_type~ : Int32)
  /// A field does not hold a valid identifier, for example an invocation ID
  /// with more than 128 characters.
  BadIdentifier(field~ : String, error~ : IdentifierError)
  /// A number is outside the range that a payload value can hold.
  NumberOutOfRange(text~ : String)
  /// A Completion holds a result and an error.
  ResultAndError(invocation_id~ : InvocationId)
  /// The peer started an invocation with an ID that it uses for an open
  /// invocation.
  InvocationIdInUse(invocation_id~ : InvocationId)
  /// The peer started an upload stream with an ID that it uses for an open
  /// stream.
  StreamIdInUse(stream_id~ : StreamId)
  /// A StreamItem or a Completion names no open invocation or stream.
  UnknownInvocation(invocation_id~ : InvocationId)
  /// A StreamItem arrived for an invocation that is not a stream.
  StreamItemForInvocation(invocation_id~ : InvocationId)
  /// A Completion with a result arrived after a StreamItem for the same ID.
  ResultAfterStreamItems(invocation_id~ : InvocationId)
  /// A hub message arrived before the handshake.
  MessageBeforeHandshake
  /// The endpoint does not accept a message of this type in its role, for
  /// example a StreamInvocation that a server sends to a client.
  UnexpectedMessage(message_type~ : Int32)
  /// A message has more bytes than the maximum message size.
  MessageTooLarge(limit~ : Int32, size~ : Int32)
}

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

///|
/// Text for people that describes the fault. A Close message holds this text.
pub fn ProtocolError::message(self : ProtocolError) -> String {
  match self {
    InvalidJson(detail~) => "The message is not valid JSON: \{detail}"
    NotAnObject => "The message is not a JSON object."
    MissingField(field~) => "The message has no field '\{field}'."
    WrongFieldType(field~, expected~) =>
      "The field '\{field}' must be \{expected}."
    UnknownField(field~) => "The message has the unknown field '\{field}'."
    UnsupportedMessageType(message_type~) =>
      "The message type \{message_type} is not supported."
    BadIdentifier(field~, error~) =>
      "The field '\{field}' is not valid: \{error.message()}"
    NumberOutOfRange(text~) => "The number \{text} is out of range."
    ResultAndError(invocation_id~) =>
      "The completion of '\{invocation_id}' holds a result and an error."
    InvocationIdInUse(invocation_id~) =>
      "The invocation ID '\{invocation_id}' is already in use."
    StreamIdInUse(stream_id~) =>
      "The stream ID '\{stream_id}' is already in use."
    UnknownInvocation(invocation_id~) =>
      "No open invocation or stream has the ID '\{invocation_id}'."
    StreamItemForInvocation(invocation_id~) =>
      "The invocation '\{invocation_id}' is not a stream, so it gets no stream item."
    ResultAfterStreamItems(invocation_id~) =>
      "The completion of the stream '\{invocation_id}' holds a result."
    MessageBeforeHandshake => "A hub message arrived before the handshake."
    UnexpectedMessage(message_type~) =>
      "A message of type \{message_type} is not expected by this endpoint."
    MessageTooLarge(limit~, size~) =>
      "The message has \{size} bytes; the maximum message size is \{limit} bytes."
  }
}

///|
/// An error that a hub method raises to send its text to the caller. The
/// Completion error holds the text as it is, also when detailed errors are off,
/// as a `HubException` does in ASP.NET Core.
#derive(Eq, Debug)
pub(all) suberror HubError {
  HubError(String)
}

///|
pub extend HubError with Eq::{equal, not_equal}

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

///|
pub impl Show for HubError with fn output(self, logger) {
  let HubError(text) = self
  logger.write_string(text)
}

///|
pub extend HubError with Show::{output, to_string}