1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF 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.apache.commons.lang3.exception; 18 19 import java.util.List; 20 import java.util.Set; 21 22 import org.apache.commons.lang3.tuple.Pair; 23 24 /** 25 * Allows the storage and retrieval of contextual information based on label-value 26 * pairs for exceptions. 27 * <p> 28 * Implementations are expected to manage the pairs in a list-style collection 29 * that keeps the pairs in the sequence of their addition. 30 * </p> 31 * 32 * @see ContextedException 33 * @see ContextedRuntimeException 34 * @since 3.0 35 */ 36 public interface ExceptionContext { 37 38 /** 39 * Adds a contextual label-value pair into this context. 40 * <p> 41 * The pair will be added to the context, independently of an already 42 * existing pair with the same label. 43 * </p> 44 * 45 * @param label the label of the item to add, {@code null} not recommended 46 * @param value the value of item to add, may be {@code null} 47 * @return {@code this}, for method chaining, not {@code null} 48 */ 49 ExceptionContext addContextValue(String label, Object value); 50 51 /** 52 * Retrieves the full list of label-value pairs defined in the contextual data. 53 * 54 * @return the list of pairs, not {@code null} 55 */ 56 List<Pair<String, Object>> getContextEntries(); 57 58 /** 59 * Retrieves the full set of labels defined in the contextual data. 60 * 61 * @return the set of labels, not {@code null} 62 */ 63 Set<String> getContextLabels(); 64 65 /** 66 * Retrieves all the contextual data values associated with the label. 67 * 68 * @param label the label to get the contextual values for, may be {@code null} 69 * @return the contextual values associated with the label, never {@code null} 70 */ 71 List<Object> getContextValues(String label); 72 73 /** 74 * Retrieves the first available contextual data value associated with the label. 75 * 76 * @param label the label to get the contextual value for, may be {@code null} 77 * @return the first contextual value associated with the label, may be {@code null} 78 */ 79 Object getFirstContextValue(String label); 80 81 /** 82 * Gets the contextualized error message based on a base message. 83 * This will add the context label-value pairs to the message. 84 * 85 * @param baseMessage the base exception message <b>without</b> context information appended 86 * @return the exception message <b>with</b> context information appended, not {@code null} 87 */ 88 String getFormattedExceptionMessage(String baseMessage); 89 90 /** 91 * Sets a contextual label-value pair into this context. 92 * <p> 93 * The pair will be added normally, but any existing label-value pair with 94 * the same label is removed from the context. 95 * </p> 96 * 97 * @param label the label of the item to add, {@code null} not recommended 98 * @param value the value of item to add, may be {@code null} 99 * @return {@code this}, for method chaining, not {@code null} 100 */ 101 ExceptionContext setContextValue(String label, Object value); 102 103 }