RCTExceptionsManager.m 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTExceptionsManager.h"
  8. #import "RCTConvert.h"
  9. #import "RCTDefines.h"
  10. #import "RCTLog.h"
  11. #import "RCTRedBox.h"
  12. #import "RCTRootView.h"
  13. @implementation RCTExceptionsManager
  14. @synthesize bridge = _bridge;
  15. RCT_EXPORT_MODULE()
  16. - (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate
  17. {
  18. if ((self = [self init])) {
  19. _delegate = delegate;
  20. }
  21. return self;
  22. }
  23. RCT_EXPORT_METHOD(reportSoftException:(NSString *)message
  24. stack:(NSArray<NSDictionary *> *)stack
  25. exceptionId:(nonnull NSNumber *)exceptionId)
  26. {
  27. [_bridge.redBox showErrorMessage:message withStack:stack];
  28. if (_delegate) {
  29. [_delegate handleSoftJSExceptionWithMessage:message stack:stack exceptionId:exceptionId];
  30. }
  31. }
  32. RCT_EXPORT_METHOD(reportFatalException:(NSString *)message
  33. stack:(NSArray<NSDictionary *> *)stack
  34. exceptionId:(nonnull NSNumber *)exceptionId)
  35. {
  36. [_bridge.redBox showErrorMessage:message withStack:stack];
  37. if (_delegate) {
  38. [_delegate handleFatalJSExceptionWithMessage:message stack:stack exceptionId:exceptionId];
  39. }
  40. static NSUInteger reloadRetries = 0;
  41. if (!RCT_DEBUG && reloadRetries < _maxReloadAttempts) {
  42. reloadRetries++;
  43. [_bridge reload];
  44. } else {
  45. NSString *description = [@"Unhandled JS Exception: " stringByAppendingString:message];
  46. NSDictionary *errorInfo = @{ NSLocalizedDescriptionKey: description, RCTJSStackTraceKey: stack };
  47. RCTFatal([NSError errorWithDomain:RCTErrorDomain code:0 userInfo:errorInfo]);
  48. }
  49. }
  50. RCT_EXPORT_METHOD(updateExceptionMessage:(NSString *)message
  51. stack:(NSArray<NSDictionary *> *)stack
  52. exceptionId:(nonnull NSNumber *)exceptionId)
  53. {
  54. [_bridge.redBox updateErrorMessage:message withStack:stack];
  55. if (_delegate && [_delegate respondsToSelector:@selector(updateJSExceptionWithMessage:stack:exceptionId:)]) {
  56. [_delegate updateJSExceptionWithMessage:message stack:stack exceptionId:exceptionId];
  57. }
  58. }
  59. // Deprecated. Use reportFatalException directly instead.
  60. RCT_EXPORT_METHOD(reportUnhandledException:(NSString *)message
  61. stack:(NSArray<NSDictionary *> *)stack)
  62. {
  63. [self reportFatalException:message stack:stack exceptionId:@-1];
  64. }
  65. @end