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