1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.thrift;
18  
19  import java.util.function.Function;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import com.linecorp.armeria.common.RequestContext;
25  import com.linecorp.centraldogma.internal.thrift.CentralDogmaException;
26  import com.linecorp.centraldogma.internal.thrift.ErrorCode;
27  
28  final class CentralDogmaExceptions {
29  
30      private static final Logger logger = LoggerFactory.getLogger(CentralDogmaExceptions.class);
31  
32      static void log(String operationName, CentralDogmaException e) {
33          final RequestContext ctx =
34                  RequestContext.mapCurrent(Function.identity(), () -> null);
35  
36          final ErrorCode errorCode = e.getErrorCode();
37          switch (errorCode) {
38              case BAD_REQUEST:
39              case PROJECT_NOT_FOUND:
40              case PROJECT_EXISTS:
41              case REPOSITORY_NOT_FOUND:
42              case REPOSITORY_EXISTS:
43              case REVISION_NOT_FOUND:
44              case REVISION_EXISTS:
45              case ENTRY_NOT_FOUND:
46              case CHANGE_CONFLICT:
47              case QUERY_FAILURE:
48                  if (logger.isDebugEnabled()) {
49                      if (ctx != null) {
50                          logger.debug("{} Exception with error code {} ({}) from: {}()",
51                                       ctx, errorCode, errorCode.getValue(), operationName, e);
52                      } else {
53                          logger.debug("Exception with error code {} ({}) from: {}()",
54                                       errorCode, errorCode.getValue(), operationName, e);
55                      }
56                  }
57                  break;
58              case REDUNDANT_CHANGE:
59              case SHUTTING_DOWN:
60                  // Do not log the stack trace for REDUNDANT_CHANGE and SHUTTING_DOWN error code
61                  // because it is expected to occur very often.
62                  if (logger.isDebugEnabled()) {
63                      if (ctx != null) {
64                          logger.debug("{} Exception with error code {} ({}) from: {}()",
65                                       ctx, errorCode, errorCode.getValue(), operationName);
66                      } else {
67                          logger.debug("Exception with error code {} ({}) from: {}()",
68                                       errorCode, errorCode.getValue(), operationName);
69                      }
70                  }
71                  break;
72              default:
73                  if (ctx != null) {
74                      logger.warn("{} Unexpected exception with error code {} ({}) from: {}()",
75                                  ctx, errorCode, errorCode.getValue(), operationName, e);
76                  } else {
77                      logger.warn("Unexpected exception with error code {} ({}) from: {}()",
78                                  errorCode, errorCode.getValue(), operationName, e);
79                  }
80          }
81      }
82  
83      private CentralDogmaExceptions() {}
84  }