diff --git a/Makefile b/Makefile index d344115b18ee85c2e42bec7dff89f51e609812e5..431bc7b2935ce3a4959ad135c38fb6298f88f8e7 100644 --- a/Makefile +++ b/Makefile @@ -34,8 +34,7 @@ doc: cd doc && $(MAKE) html style: - # Disabled until https://github.com/florianschanda/miss_hit/issues/224 is resolved. - # mh_style --fix --octave src doc + mh_style --fix --octave src doc clang-format-11 --dry-run --verbose --Werror $(shell find test/ src/ -type f -iname "*.cpp" -o -iname "*.hpp" -o -iname "*.h" -o -iname "*.h.in") .PHONY: style diff --git a/README_SETUP.md b/README_SETUP.md index 12cc034607da0c549d18d1e772a4d23057eb2a4f..8d00d43f1ef616b48fd906437f88acade005851c 100644 --- a/README_SETUP.md +++ b/README_SETUP.md @@ -5,7 +5,8 @@ This package requires the following software: - conan: `pip install conan` -- libcaosdb: See https://gitlab.indiscale.com/caosdb/src/caosdb-cpplib for further instructions. +- libcaosdb installed with Conan: See https://gitlab.indiscale.com/caosdb/src/caosdb-cpplib for + further instructions. - For running this library, a valid libcaosdb configuration is needed, for example a `.caosdb_client.json` file in your home directory or current working directory. Please look at [`README_SETUP.md` in caosdb-cpplib](https://gitlab.indiscale.com/caosdb/src/caosdb-cpplib/-/blob/main/README_SETUP.md) for more information. @@ -50,4 +51,3 @@ static website. If you rather would like to use the native, - `sphinx` - `sphinx-autoapi` - `recommonmark` - diff --git a/conanfile.txt b/conanfile.txt index e89a593b5f6388ac539975c6ae0ed808a900ecc3..013b9213e22b751bb8495de192137d6d73c9485e 100644 --- a/conanfile.txt +++ b/conanfile.txt @@ -1,5 +1,5 @@ [requires] -caosdb/0.0.5 +caosdb/0.0.6 [generators] cmake diff --git a/src/caosdbClass.m b/src/caosdbClass.m index eba38525652276b47b73c77691206dae058962f3..869845eac8b3321117f61525d5e714431bec2697 100644 --- a/src/caosdbClass.m +++ b/src/caosdbClass.m @@ -17,11 +17,12 @@ classdef caosdbClass < handle function res = info(obj) disp(["connection: >", obj.connection, "<"]); try - info_result = _info(obj.connection); + info_result = maox_info(obj.connection); res = info_result; catch disp("some error!") - res = NA + disp(lasterror()) + res = NA; end_try_catch endfunction endmethods diff --git a/src/lib/maoxdb.cpp b/src/lib/maoxdb.cpp new file mode 100644 index 0000000000000000000000000000000000000000..780b762eed975773832dc5ca22b8ae70cea12123 --- /dev/null +++ b/src/lib/maoxdb.cpp @@ -0,0 +1,53 @@ +/* + * ** header v3.0 + * This file is a part of the CaosDB Project. + * + * Copyright (C) 2021 IndiScale GmbH <info@indiscale.com> + * Copyright (C) 2021 Daniel Hornung <d.hornung@indiscale.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + * ** end header + */ + +/** + * Mostly utility functions for MEX files interacting with libcaosdb. + */ + +#include "maoxdb.hpp" +#include "caosdb/status_code.h" + +namespace maoxdb { +using std::string; + +/** + * Extract useful strings from an Exception: ID and description. + */ +std::pair<string, string> exceptionToMessage(const caosdb::exceptions::Exception &exc) { + + string id = std::to_string(exc.GetCode()); + string text = caosdb::get_status_description(exc.GetCode()) + "\n" + exc.what(); + + return std::pair<string, string>(id, text); +} + +/** + * Just throw the error. + */ +void throwOctException(const caosdb::exceptions::Exception &exc) { + std::pair<string, string> excContent = exceptionToMessage(exc); + mexErrMsgIdAndTxt(excContent.first, excContent.second); +} + +} diff --git a/src/lib/maoxdb.hpp b/src/lib/maoxdb.hpp index c2e93c31e89d20d2a1f8cd173f52eefa11eec807..4749e41df4712be06e9e853e1c0ecab025a2297a 100644 --- a/src/lib/maoxdb.hpp +++ b/src/lib/maoxdb.hpp @@ -1,8 +1,11 @@ #ifndef MAOXDB_H #define MAOXDB_H +#include "caosdb/exceptions.h" +#include "caosdb/status_code.h" #include "mex.h" #include "mexproto.h" +#include <string> // Macros ///////////////////////////////////////////////////////////////////// @@ -21,6 +24,7 @@ * scalar_data[0] = value; * mxSetData(scalar, scalar_data); * + * TODO: This macro could be rewritten as a function now. */ #define mxSCALAR(type, name, value) \ mxCreateNumericMatrix(1, 1, mx##type##_CLASS, mxREAL); \ @@ -29,7 +33,37 @@ mxSetData(name, __##name##_data) // Utility functions ////////////////////////////////////////////////////////// +namespace maoxdb { +using std::string; +// doxy void test(); +/** + * @brief Handle a CaosDB Exception and transform into error code and message. + * + * @details This function does all the processing to transform the information in an Exception + * object into a form that can conveniently be handled by Octave via the mexErrMsgIdAndTxt function. + * + * @param exc The Exception object. + * + * @return pair<string msgId, string msgText> The message ID and message text which can be passed to + * Octave. + */ +std::pair<string, string> exceptionToMessage(const caosdb::exceptions::Exception &exc); + +/** + * @brief Throw a CaosDB Exception inside Octave. + * + * @details This function does all the processing to transform the information in an Exception + * object into a form that is accepted by Octave's error handling.d + * + * Internally, this function uses the exceptionToMessage function. + * + * @param exc The Exception object. + */ +void throwOctException(const caosdb::exceptions::Exception &exc); + +} // maoxdb + #endif /* MAOXDB_H */ diff --git a/src/lib/test.cpp b/src/lib/test.cpp index 6b5449cb35bb6825564c644e30039fb3506e272e..41d1bb70d8ea96ec861e2807fab80487f7b604f5 100644 --- a/src/lib/test.cpp +++ b/src/lib/test.cpp @@ -2,7 +2,11 @@ #include "mex.h" #include "mexproto.h" +namespace maoxdb { + void test() { // do nothing. auto *mx_str = mxCreateString("Hello World!"); } + +} // maoxdb diff --git a/src/private/_caosdb.cpp b/src/private/maox_caosdb.cpp similarity index 100% rename from src/private/_caosdb.cpp rename to src/private/maox_caosdb.cpp diff --git a/src/private/_info.cpp b/src/private/maox_info.cpp similarity index 96% rename from src/private/_info.cpp rename to src/private/maox_info.cpp index 26b294686765d3aeae28a8cd5617b92cd2528e6f..de36999e5f83caac106fe218ea73cce3c2956dd9 100644 --- a/src/private/_info.cpp +++ b/src/private/maox_info.cpp @@ -64,9 +64,8 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { try { auto info_struct = info(conn_name); plhs[0] = info_struct; - } catch (const std::runtime_error &err) { + } catch (const caosdb::exceptions::Exception &exc) { mexPrintf("Some exception!"); - mexErrMsgIdAndTxt("someID", err.what()); + maoxdb::throwOctException(exc); } - test(); } diff --git a/test/test_caosdb.m b/test/test_caosdb.m index 6ce8788081c534b8efc72bae0ff76aae8cce3cee..27928c7a4d85e930c1c9552049401447620c5094 100644 --- a/test/test_caosdb.m +++ b/test/test_caosdb.m @@ -7,4 +7,4 @@ function test_suite=test_caosdb function test_print_version v = caosdb("--version") - assertEqual(v, "v0.1 (libcaosdb v0.0.5)") + assertEqual(v, "v0.1 (libcaosdb v0.0.6)")