• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #ifdef GRPC_CFSTREAM
22 #include <CoreFoundation/CoreFoundation.h>
23 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/string_util.h>
26 
27 #include "src/core/lib/iomgr/error.h"
28 
29 #define MAX_ERROR_DESCRIPTION 256
30 
grpc_error_create_from_cferror(const char * file,int line,void * arg,const char * custom_desc)31 grpc_error* grpc_error_create_from_cferror(const char* file, int line,
32                                            void* arg, const char* custom_desc) {
33   CFErrorRef error = static_cast<CFErrorRef>(arg);
34   char buf_domain[MAX_ERROR_DESCRIPTION];
35   char buf_desc[MAX_ERROR_DESCRIPTION];
36   char* error_msg;
37   CFErrorDomain domain = CFErrorGetDomain((error));
38   CFIndex code = CFErrorGetCode((error));
39   CFStringRef desc = CFErrorCopyDescription((error));
40   CFStringGetCString(domain, buf_domain, MAX_ERROR_DESCRIPTION,
41                      kCFStringEncodingUTF8);
42   CFStringGetCString(desc, buf_desc, MAX_ERROR_DESCRIPTION,
43                      kCFStringEncodingUTF8);
44   gpr_asprintf(&error_msg, "%s (error domain:%s, code:%ld, description:%s)",
45                custom_desc, buf_domain, code, buf_desc);
46   CFRelease(desc);
47   grpc_error* return_error = grpc_error_create(
48       file, line, grpc_slice_from_copied_string(error_msg), NULL, 0);
49   gpr_free(error_msg);
50   return return_error;
51 }
52 #endif /* GRPC_CFSTREAM */
53