Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions djinni/objc/DJIError.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@

#pragma once

#include <exception>

namespace djinni {

typedef NSException * (* ExceptionTranslatorFunc)(const std::exception & e);

// Sets custom function to perform the translation of C++ exception to NSException
void setCustomExceptionTranslator(ExceptionTranslatorFunc func);

// Throws an exception for an unimplemented method call.
[[noreturn]] void throwUnimplemented(const char * ctx, NSString * msg);

Expand Down
16 changes: 13 additions & 3 deletions djinni/objc/DJIError.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
// clang-format off
#include <Foundation/Foundation.h>
#include "DJIError.h"
#include <exception>
// clang-format on
static_assert(__has_feature(objc_arc), "Djinni requires ARC to be enabled for this file");

namespace djinni {

ExceptionTranslatorFunc customTranslatorFunc = nullptr;

void setCustomExceptionTranslator(ExceptionTranslatorFunc func) {
customTranslatorFunc = func;
}

[[noreturn]] __attribute__((weak)) void throwUnimplemented(const char * /*ctx*/, NSString * message) {
[NSException raise:NSInternalInconsistencyException format:@"Unimplemented: %@", message];
__builtin_unreachable();
Expand All @@ -31,8 +36,13 @@
try {
throw;
} catch (const std::exception & e) {
NSString *message = [NSString stringWithCString:e.what() encoding:NSUTF8StringEncoding];
[NSException raise:message format:@"%@", message];
if (customTranslatorFunc) {
NSException *exception = customTranslatorFunc(e);
[exception raise];
} else {
NSString *message = [NSString stringWithCString:e.what() encoding:NSUTF8StringEncoding];
[NSException raise:message format:@"%@", message];
}
__builtin_unreachable();
}
}
Expand Down
Loading