All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.commons.error.IError Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014-2024 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.helger.commons.error;

import java.time.LocalDateTime;
import java.util.Locale;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.helger.commons.annotation.MustImplementEqualsAndHashcode;
import com.helger.commons.annotation.Nonempty;
import com.helger.commons.error.level.IHasErrorLevelComparable;
import com.helger.commons.error.text.IHasErrorText;
import com.helger.commons.location.ILocation;
import com.helger.commons.location.SimpleLocation;

/**
 * Common interface for single errors and resource errors.
 *
 * @author Philip Helger
 * @since 8.5.0
 */
@MustImplementEqualsAndHashcode
public interface IError extends IHasErrorLevelComparable , IHasErrorID, IHasErrorField
{
  /**
   * @return The date and time when the error occurred. Defaults to
   *         null for backwards compatibility.
   * @since 10.1.7
   */
  @Nullable
  default LocalDateTime getErrorDateTime ()
  {
    return null;
  }

  /**
   * Check if a error date time is present.
   *
   * @return true if error date time information is present,
   *         false otherwise.
   * @see #getErrorDateTime()
   * @since 10.1.7
   */
  default boolean hasErrorDateTime ()
  {
    return getErrorDateTime () != null;
  }

  /**
   * {@inheritDoc}
   */
  default String getErrorID ()
  {
    return null;
  }

  /**
   * @return The field for which the error occurred. May be null.
   */
  @Nullable
  default String getErrorFieldName ()
  {
    return null;
  }

  /**
   * @return The non-null location of the error. Use
   *         {@link SimpleLocation#NO_LOCATION} to indicate no location is
   *         available.
   * @see #hasErrorLocation()
   */
  @Nonnull
  default ILocation getErrorLocation ()
  {
    return SimpleLocation.NO_LOCATION;
  }

  /**
   * Check if a reasonable error location is present.
   *
   * @return true if location information is present,
   *         false otherwise.
   * @see #getErrorLocation()
   */
  default boolean hasErrorLocation ()
  {
    return getErrorLocation ().isAnyInformationPresent ();
  }

  /**
   * @return The contained error message text. May be null.
   * @see #getErrorText(Locale)
   */
  @Nullable
  default IHasErrorText getErrorTexts ()
  {
    return null;
  }

  /**
   * Get the error message of this error.
   *
   * @param aContentLocale
   *        The locale to be used in case the error text is available in
   *        multiple languages.
   * @return The message of this form error. May be null in case no
   *         error text is available or if the passed Locale is not supported.
   * @see #getErrorTexts()
   */
  @Nullable
  default String getErrorText (@Nonnull final Locale aContentLocale)
  {
    final IHasErrorText aErrorText = getErrorTexts ();
    return aErrorText == null ? null : aErrorText.getDisplayText (aContentLocale);
  }

  /**
   * @return The linked exception or null if no such exception is
   *         available.
   * @see #hasLinkedException()
   * @see #getLinkedExceptionMessage()
   * @see #getLinkedExceptionStackTrace()
   * @see #getLinkedExceptionCause()
   */
  @Nullable
  default Throwable getLinkedException ()
  {
    return null;
  }

  /**
   * @return true if a linked exception is present,
   *         false if not.
   * @see #getLinkedException()
   */
  default boolean hasLinkedException ()
  {
    return getLinkedException () != null;
  }

  /**
   * @return The message of the linked exception or null if no such
   *         exception is available.
   * @see #getLinkedException()
   */
  @Nullable
  default String getLinkedExceptionMessage ()
  {
    final Throwable t = getLinkedException ();
    return t == null ? null : t.getMessage ();
  }

  /**
   * @return The stack trace of the linked exception or null if no
   *         such exception is available.
   * @see #getLinkedException()
   */
  @Nullable
  default StackTraceElement [] getLinkedExceptionStackTrace ()
  {
    final Throwable t = getLinkedException ();
    return t == null ? null : t.getStackTrace ();
  }

  /**
   * @return The cause of the linked exception or null if no such
   *         exception is available.
   * @see #getLinkedException()
   */
  @Nullable
  default Throwable getLinkedExceptionCause ()
  {
    final Throwable t = getLinkedException ();
    return t == null ? null : t.getCause ();
  }

  /**
   * Get the error as a string representation, including error ID, error
   * location, error text and the linked exception.
   *
   * @param aContentLocale
   *        Locale to resolve the error text
   * @return The default string representation
   * @see ErrorTextProvider#DEFAULT
   * @see #getAsStringLocaleIndepdent()
   */
  @Nonnull
  @Nonempty
  default String getAsString (@Nonnull final Locale aContentLocale)
  {
    return ErrorTextProvider.DEFAULT.getErrorText (this, aContentLocale);
  }

  /**
   * Get the error as a string representation, including error ID, error
   * location, error text and the linked exception.
   *
   * @return The default string representation in the default locale.
   * @see ErrorTextProvider#DEFAULT
   * @see #getAsString(Locale)
   * @since 11.1.4
   */
  @Nonnull
  @Nonempty
  default String getAsStringLocaleIndepdent ()
  {
    return getAsString (Locale.ROOT);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy