FBErrorBuilder.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import "FBErrorBuilder.h"
  10. static NSString *const FBWebServerErrorDomain = @"com.facebook.WebDriverAgent";
  11. @interface FBErrorBuilder ()
  12. @property (nonatomic, copy) NSString *errorDescription;
  13. @property (nonatomic, strong) NSError *innerError;
  14. @end
  15. @implementation FBErrorBuilder
  16. + (instancetype)builder
  17. {
  18. return [FBErrorBuilder new];
  19. }
  20. - (instancetype)withDescription:(NSString *)description
  21. {
  22. self.errorDescription = description;
  23. return self;
  24. }
  25. - (instancetype)withDescriptionFormat:(NSString *)format, ...
  26. {
  27. va_list argList;
  28. va_start(argList, format);
  29. self.errorDescription = [[NSString alloc] initWithFormat:format arguments:argList];
  30. va_end(argList);
  31. return self;
  32. }
  33. - (instancetype)withInnerError:(NSError *)error
  34. {
  35. self.innerError = error;
  36. return self;
  37. }
  38. - (BOOL)buildError:(NSError **)errorOut
  39. {
  40. if (errorOut) {
  41. *errorOut = [self build];
  42. }
  43. return NO;
  44. }
  45. - (NSError *)build
  46. {
  47. return
  48. [NSError errorWithDomain:FBWebServerErrorDomain
  49. code:1
  50. userInfo:[self buildUserInfo]
  51. ];
  52. }
  53. - (NSDictionary *)buildUserInfo
  54. {
  55. NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
  56. if (self.errorDescription) {
  57. userInfo[NSLocalizedDescriptionKey] = self.errorDescription;
  58. }
  59. if (self.innerError) {
  60. userInfo[NSUnderlyingErrorKey] = self.innerError;
  61. }
  62. return userInfo.copy;
  63. }
  64. @end