Skip to content

Usage: Message Errors

Adrian edited this page Feb 28, 2024 · 4 revisions

Handling Errors

For error handling, peekaboo uses an enum of Error conditions, provided by at\peekaboo\MessageError. These error cases are thrown as MessageExceptions. You can refer to the at\exceptable package to understand more about how this works.*

* Peekaboo will use the Exceptable library if it is installed, but does not directly depend on it in order to avoid circular dependencies. If it is not installed, peekaboo defines stub implementations.

This means you can catch MessageExceptions, and compare them to the MessageError case(s) you're interested in:

use at\peekaboo\ {
  MessageError,
  MessageException
};

try {
  $message = $hasMessages->makeMessage($key, $context);
} catch (MessageException $e) {
  if ($e->is(MessageError::FormatFailed)) {
    // log it, maybe provide a default message, etc.
  } else {
    throw $e;
  }
}

error cases

  • MessageError::FormatFailed

    This error is thrown when formatting a message fails. It provides you with debugging info, including the locale, format string, context, and the error code and message from the intl extension (if one exists).

    error formatting message: (31) U_INPUT_TOO_LONG_ERROR
      locale: en_US
      format: darkness is upon me, and {tm} night must fall
      context: {"tm":"soon"}
    
  • MessageError::NotAMessage

    This error is thrown when looking up a format by key, but the value found is not a message format (e.g., looking up foo.bar but bar is actually another message bundle).

    value at YourClass:foo.bar is not a message format string
    
  • MessageError::BadMessages

    This error is thrown when trying to look up a message format on a peekaboo class's MESSAGES const, but the value you declared is malformed or not an array. (This is a bug for you to fix ;)

    MakesMessages::MESSAGES must be an array of message formats; string declared
    

Clone this wiki locally