FSTUsageValidation.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <Foundation/Foundation.h>
  17. NS_ASSUME_NONNULL_BEGIN
  18. #if __cplusplus
  19. extern "C" {
  20. #endif
  21. /** Helper for creating a general exception for invalid usage of an API. */
  22. NSException *FSTInvalidUsage(NSString *exceptionName, NSString *format, ...);
  23. /**
  24. * Macro to throw exceptions in response to API usage errors. Avoids the lint warning you usually
  25. * get when using @throw and (unlike a function) doesn't trigger warnings about not all codepaths
  26. * returning a value.
  27. *
  28. * Exceptions should only be used for programmer errors made by consumers of the SDK, e.g.
  29. * invalid method arguments.
  30. *
  31. * For recoverable runtime errors, use NSError**.
  32. * For internal programming errors, use FSTFail().
  33. */
  34. #define FSTThrowInvalidUsage(exceptionName, format, ...) \
  35. do { \
  36. @throw FSTInvalidUsage(exceptionName, format, ##__VA_ARGS__); \
  37. } while (0)
  38. #define FSTThrowInvalidArgument(format, ...) \
  39. do { \
  40. @throw FSTInvalidUsage(@"FIRInvalidArgumentException", format, ##__VA_ARGS__); \
  41. } while (0)
  42. #if __cplusplus
  43. } // extern "C"
  44. #endif
  45. NS_ASSUME_NONNULL_END