RuggedInternalError.java

  1. /* Copyright 2013-2022 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.rugged.errors;

  18. import java.text.MessageFormat;
  19. import java.util.Locale;

  20. import org.hipparchus.exception.Localizable;
  21. import org.hipparchus.exception.LocalizedException;


  22. /** Extension of {@link java.lang.Runtime} with localized message for internal errors only.
  23.  * @since 2.1
  24.  */
  25. public class RuggedInternalError extends RuntimeException implements LocalizedException {

  26.     /** Serializable UID. */
  27.     private static final long serialVersionUID = 20190305L;

  28.     /** Format specifier (to be translated). */
  29.     private final Localizable specifier = RuggedMessages.INTERNAL_ERROR;

  30.     /** Parts to insert in the format (no translation). */
  31.     private final String[] parts        = new String[] {
  32.         "https://gitlab.orekit.org/orekit/rugged/issues"
  33.     };

  34.     /** Create an exception with localized message.
  35.      * @param cause underlying cause
  36.      */
  37.     public RuggedInternalError(final Throwable cause) {
  38.         super(cause);
  39.     }

  40.     /** {@inheritDoc} */
  41.     @Override
  42.     public String getMessage(final Locale locale) {
  43.         return buildMessage(locale);
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public String getMessage() {
  48.         return buildMessage(Locale.US);
  49.     }

  50.     /** {@inheritDoc} */
  51.     @Override
  52.     public String getLocalizedMessage() {
  53.         return buildMessage(Locale.getDefault());
  54.     }

  55.     /** {@inheritDoc} */
  56.     @Override
  57.     public Localizable getSpecifier() {
  58.         return specifier;
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public Object[] getParts() {
  63.         return parts.clone();
  64.     }

  65.     /**
  66.      * Builds a message string by from a pattern and its arguments.
  67.      * @param locale Locale in which the message should be translated
  68.      * @return a message string
  69.      */
  70.     private String buildMessage(final Locale locale) {
  71.         return new MessageFormat(specifier.getLocalizedString(locale), locale).format(parts);
  72.     }

  73. }