/**
   * Wraps an {@link Exception} and creates a new {@link ApplicationException} with a localized
   * message and the given {@link ErrorCode}.
   *
   * @param ex the exception.
   * @param code the error code.
   * @return the application exception.
   */
  protected ApplicationException wrapApplicationException(Exception ex, ErrorCode code) {
    String pattern =
        messageSource.getMessage(code.getMessageKey(), null, code.getMessageKey(), null);

    return ApplicationException.wrap(ex, code, pattern);
  }
  /**
   * Creates an {@link ApplicationException} exception with a localized message.
   *
   * @param code unique error code.
   * @return the application exception.
   */
  protected ApplicationException createApplicationException(ErrorCode code) {
    String pattern =
        messageSource.getMessage(code.getMessageKey(), null, code.getMessageKey(), null);

    return ApplicationException.create(code, pattern);
  }