ErrorHandler.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ErrorHandler.h"
  21. #import "ThreemaFramework/ThreemaFramework-swift.h"
  22. #ifdef DEBUG
  23. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  24. #else
  25. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  26. #endif
  27. @interface ErrorHandler ()
  28. @end
  29. static ErrorHandler *errorHandler;
  30. @implementation ErrorHandler
  31. + (instancetype)errorHandler {
  32. return [[ErrorHandler alloc] init];
  33. }
  34. + (void)abortWithError:(NSError *)error {
  35. [self abortWithError:error additionalText:nil];
  36. }
  37. + (void)abortWithError:(NSError *)error additionalText:(NSString*)additionalText {
  38. DDLogError(@"Aborting: unresolved error %@, %@", error, [error userInfo]);
  39. NSString *localizedMessage = [NSString stringWithFormat:@"%@ (%@ %ld)", [error localizedDescription], error.domain, (long)error.code];
  40. if (additionalText != nil) {
  41. localizedMessage = [NSString stringWithFormat:@"%@\n\n%@", localizedMessage, additionalText];
  42. }
  43. [ErrorHandler abortWithTitle:NSLocalizedString(@"error_abort", nil) message:localizedMessage];
  44. }
  45. + (void)abortWithTitle:(NSString *)title message:(NSString*)message {
  46. errorHandler = [ErrorHandler errorHandler];
  47. Class UIApplicationClass = NSClassFromString(@"UIApplication");
  48. if(!UIApplicationClass || ![UIApplicationClass respondsToSelector:@selector(sharedApplication)]) {
  49. return;
  50. }
  51. UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
  52. if (application == nil) {
  53. return;
  54. } else if (application.keyWindow.rootViewController == nil) {
  55. UIViewController *dummyViewController = [[UIViewController alloc] init];
  56. application.keyWindow.rootViewController = dummyViewController;
  57. }
  58. [UIAlertTemplate showAlertWithOwner:application.keyWindow.rootViewController title:title message:message actionOk:nil];
  59. }
  60. @end