xref: /aosp_15_r20/external/cronet/third_party/netty-tcnative/src/c/error.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /*
2  * Copyright 2016 The Netty Project
3  *
4  * The Netty Project 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  *   http://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 /* Licensed to the Apache Software Foundation (ASF) under one or more
17  * contributor license agreements.  See the NOTICE file distributed with
18  * this work for additional information regarding copyright ownership.
19  * The ASF licenses this file to You under the Apache License, Version 2.0
20  * (the "License"); you may not use this file except in compliance with
21  * the License.  You may obtain a copy of the License at
22  *
23  *     http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31 
32 #include "tcn.h"
33 #include "apr_strings.h"
34 
35 /*
36  * Convenience function to help throw an java.lang.Exception.
37  */
tcn_ThrowException(JNIEnv * env,const char * msg)38 void tcn_ThrowException(JNIEnv *env, const char *msg)
39 {
40     jclass javaExceptionClass;
41 
42     javaExceptionClass = (*env)->FindClass(env, "java/lang/Exception");
43     if (javaExceptionClass == NULL) {
44         fprintf(stderr, "Cannot find java/lang/Exception class\n");
45         return;
46     }
47     (*env)->ThrowNew(env, javaExceptionClass, msg);
48     (*env)->DeleteLocalRef(env, javaExceptionClass);
49 
50 }
51 
tcn_Throw(JNIEnv * env,const char * fmt,...)52 void tcn_Throw(JNIEnv *env, const char *fmt, ...)
53 {
54     char msg[TCN_BUFFER_SZ] = {'\0'};
55     va_list ap;
56 
57     va_start(ap, fmt);
58     apr_vsnprintf(msg, TCN_BUFFER_SZ, fmt, ap);
59     tcn_ThrowException(env, msg);
60     va_end(ap);
61 }
62 
tcn_ThrowAPRException(JNIEnv * e,apr_status_t err)63 void tcn_ThrowAPRException(JNIEnv *e, apr_status_t err)
64 {
65     char serr[512] = {0};
66 
67     apr_strerror(err, serr, 512);
68     tcn_ThrowException(e, serr);
69 }
70