1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef JS_COMMON_H 17 #define JS_COMMON_H 18 #include "hilog/log.h" 19 namespace OHOS::ObjectStore { 20 class Constants { 21 public: 22 static constexpr const char *CHANGE = "change"; 23 static constexpr const char *STATUS = "status"; 24 }; 25 #define OBJECT_REVT_NOTHING 26 27 #define NOT_MATCH_RETURN(condition, res) \ 28 do { \ 29 if (!(condition)) { \ 30 LOG_ERROR("error! %{public}s", #condition); \ 31 return res; \ 32 } \ 33 } while (0) 34 35 #define NOT_MATCH_RETURN_VOID(condition) \ 36 NOT_MATCH_RETURN(condition, OBJECT_REVT_NOTHING) 37 38 #define NOT_MATCH_RETURN_NULL(condition) \ 39 NOT_MATCH_RETURN(condition, nullptr) 40 41 #define NOT_MATCH_RETURN_FALSE(condition) \ 42 NOT_MATCH_RETURN(condition, false) 43 44 #define NOT_MATCH_GOTO_ERROR(condition) \ 45 { \ 46 if (!(condition)) { \ 47 LOG_ERROR("error! %{public}s", #condition); \ 48 goto ERROR; \ 49 } \ 50 } 51 52 #define INVALID_API_THROW_ERROR(assertion) \ 53 do { \ 54 if (!(assertion)) { \ 55 std::shared_ptr<DeviceNotSupportedError> apiError = std::make_shared<DeviceNotSupportedError>(); \ 56 ctxt->SetError(apiError); \ 57 ctxt->status = napi_generic_failure; \ 58 return; \ 59 } \ 60 } while (0) 61 62 #define INVALID_STATUS_THROW_ERROR(assertion, msg) \ 63 do { \ 64 if (!(assertion)) { \ 65 std::shared_ptr<InnerError> innerError = std::make_shared<InnerError>(); \ 66 ctxt->SetError(innerError); \ 67 ctxt->status = napi_generic_failure; \ 68 ctxt->message = msg; \ 69 return; \ 70 } \ 71 } while (0) 72 } // namespace OHOS::ObjectStore 73 #endif // JS_COMMON_H