NSError+RQExtension.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // NSError+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/23.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "NSError+RQExtension.h"
  9. // The domain for errors originating from RQModel.
  10. static NSString * const RQModelErrorDomain = @"RQModelErrorDomain";
  11. // An exception was thrown and caught.
  12. static const NSInteger RQModelErrorExceptionThrown = 1;
  13. // Associated with the NSException that was caught.
  14. static NSString * const RQModelThrownExceptionErrorKey = @"RQModelThrownException";
  15. @implementation NSError (RQExtension)
  16. + (instancetype)rq_modelErrorWithException:(NSException *)exception {
  17. NSParameterAssert(exception != nil);
  18. NSDictionary *userInfo = @{
  19. NSLocalizedDescriptionKey: exception.description,
  20. NSLocalizedFailureReasonErrorKey: exception.reason,
  21. RQModelThrownExceptionErrorKey: exception
  22. };
  23. return [NSError errorWithDomain:RQModelErrorDomain code:RQModelErrorExceptionThrown userInfo:userInfo];
  24. }
  25. + (NSString *)rq_tipsFromError:(NSError *)error{
  26. if (!error) return nil;
  27. NSString *tipStr = nil;
  28. /// 这里需要处理HTTP请求的错误
  29. if (error.domain) {
  30. tipStr = error.localizedFailureReason;
  31. } else {
  32. tipStr = error.localizedDescription;
  33. }
  34. return tipStr;
  35. }
  36. @end