• 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 <string>
23 
24 #include "absl/strings/str_format.h"
25 
26 #include <CoreFoundation/CoreFoundation.h>
27 
28 #include <grpc/support/alloc.h>
29 
30 #include "src/core/lib/iomgr/error.h"
31 
32 #define MAX_ERROR_DESCRIPTION 256
33 
grpc_error_create_from_cferror(const char * file,int line,void * arg,const char * custom_desc)34 grpc_error* grpc_error_create_from_cferror(const char* file, int line,
35                                            void* arg, const char* custom_desc) {
36   CFErrorRef error = static_cast<CFErrorRef>(arg);
37   char buf_domain[MAX_ERROR_DESCRIPTION];
38   char buf_desc[MAX_ERROR_DESCRIPTION];
39   CFErrorDomain domain = CFErrorGetDomain((error));
40   CFIndex code = CFErrorGetCode((error));
41   CFStringRef desc = CFErrorCopyDescription((error));
42   CFStringGetCString(domain, buf_domain, MAX_ERROR_DESCRIPTION,
43                      kCFStringEncodingUTF8);
44   CFStringGetCString(desc, buf_desc, MAX_ERROR_DESCRIPTION,
45                      kCFStringEncodingUTF8);
46   std::string error_msg =
47       absl::StrFormat("%s (error domain:%s, code:%ld, description:%s)",
48                       custom_desc, buf_domain, code, buf_desc);
49   CFRelease(desc);
50   return grpc_error_create(
51       file, line, grpc_slice_from_copied_string(error_msg.c_str()), NULL, 0);
52 }
53 #endif /* GRPC_CFSTREAM */
54