MessageSourceException constructor

MessageSourceException(
  1. String message, {
  2. String? code,
  3. Locale? locale,
  4. String? resource,
  5. Object? cause,
})

An exception that occurs when resolving messages from a MessageSource.

This exception provides additional context beyond a simple error message:

  • code: the message key that failed to resolve.
  • locale: the locale that was requested when the error occurred.
  • resource: the underlying resource (e.g., file, bundle, or database) involved.
  • cause: the original exception that triggered this error, if any.

This is especially useful in internationalization (i18n) and configuration systems where message lookup may fail due to missing keys, unsupported locales, or inaccessible resources.

Example

void loadMessage(String key, Locale locale) {
  throw MessageSourceException(
    "Message key not found",
    code: key,
    locale: locale,
    resource: "messages_en.properties",
  );
}

try {
  loadMessage("missing.key", Locale("en"));
} catch (e) {
  print(e);
  // Output:
  // MessageSourceException: Message key not found [code=missing.key] [locale=en] [resource=messages_en.properties]
}

Implementation

MessageSourceException(
  String message, {
  this.code,
  this.locale,
  this.resource,
  Object? cause,
}) : super(message, cause: cause);