1 /** 2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * SPDX-License-Identifier: Apache-2.0. 4 */ 5 package software.amazon.awssdk.crt; 6 7 import java.util.regex.Matcher; 8 import java.util.regex.Pattern; 9 10 /** 11 * This exception will be thrown by any exceptional cases encountered within 12 * the JNI bindings to the AWS Common Runtime 13 */ 14 public class CrtRuntimeException extends RuntimeException { 15 16 private static final long serialVersionUID = 0; // Shut up linters 17 18 static { CRT()19 new CRT(); 20 } 21 22 public final int errorCode; 23 public final String errorName; 24 25 private final static Pattern crtExFormat = Pattern.compile("aws_last_error: (.+)\\(([-0-9]+)\\),"); 26 27 /** 28 * Constructor for CRT exceptions not due to native errors 29 * @param msg exception message 30 */ CrtRuntimeException(String msg)31 public CrtRuntimeException(String msg) { 32 super(msg); 33 34 Matcher matcher = crtExFormat.matcher(msg); 35 if (matcher.find()) { 36 errorName = matcher.group(1); 37 errorCode = Integer.parseInt(matcher.group(2)); 38 } else { 39 errorCode = -1; 40 errorName = "UNKNOWN"; 41 } 42 } 43 44 /** 45 * @deprecated use CrtRuntimeException(int errorCode) 46 * @param errorCode native error code detailing the reason for the exception 47 * @param errorName name of native error code 48 */ 49 @Deprecated CrtRuntimeException(int errorCode, String errorName)50 public CrtRuntimeException(int errorCode, String errorName) { 51 super(CRT.awsErrorString(errorCode)); 52 this.errorCode = errorCode; 53 this.errorName = errorName; 54 } 55 56 /** 57 * Constructor for Crt exceptions due to native errors 58 * @param errorCode native error code detailing the reason for the exception 59 */ CrtRuntimeException(int errorCode)60 public CrtRuntimeException(int errorCode) { 61 super(CRT.awsErrorString(errorCode)); 62 this.errorCode = errorCode; 63 this.errorName = CRT.awsErrorName(errorCode); 64 } 65 66 @Override toString()67 public String toString() { 68 return String.format("%s %s(%d)", super.toString(), errorName, errorCode); 69 } 70 }; 71