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 <string>
25 
26 #include "absl/strings/str_format.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_handle grpc_error_create_from_cferror(const char* file, int line,
35                                                  void* arg,
36                                                  const char* custom_desc) {
37   CFErrorRef error = static_cast<CFErrorRef>(arg);
38   char buf_domain[MAX_ERROR_DESCRIPTION];
39   char buf_desc[MAX_ERROR_DESCRIPTION];
40   CFErrorDomain domain = CFErrorGetDomain((error));
41   CFIndex code = CFErrorGetCode((error));
42   CFStringRef desc = CFErrorCopyDescription((error));
43   CFStringGetCString(domain, buf_domain, MAX_ERROR_DESCRIPTION,
44                      kCFStringEncodingUTF8);
45   CFStringGetCString(desc, buf_desc, MAX_ERROR_DESCRIPTION,
46                      kCFStringEncodingUTF8);
47   std::string error_msg =
48       absl::StrFormat("%s (error domain:%s, code:%ld, description:%s)",
49                       custom_desc, buf_domain, code, buf_desc);
50   CFRelease(desc);
51   return StatusCreate(absl::StatusCode::kUnknown, error_msg,
52                       grpc_core::DebugLocation(file, line), {});
53 }
54 #endif  // GRPC_CFSTREAM
55