Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bindings/C): Implement error with error message #3250

Merged
merged 21 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/bindings_c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ jobs:
- name: Check diff
run: git diff --exit-code

- name: Build and Run BDD tests
- name: Build and Run tests
working-directory: "bindings/c"
run: make test

- name: Build and Run memory-leak tests
working-directory: "bindings/c"
run: make memory_leak
7 changes: 7 additions & 0 deletions bindings/c/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ build/
docs/

*.o

examples/*
!examples/
!examples/*.h
!examples/*.c
!examples/*.cc
!examples/*.cpp
8 changes: 8 additions & 0 deletions bindings/c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ build:
test:
$(CXX) tests/bdd.cpp -o $(OBJ_DIR)/bdd $(CXXFLAGS) $(LDFLAGS) $(LIBS)
$(CXX) tests/list.cpp -o $(OBJ_DIR)/list $(CXXFLAGS) $(LDFLAGS) $(LIBS)
$(CXX) tests/error_msg.cpp -o $(OBJ_DIR)/error_msg $(CXXFLAGS) $(LDFLAGS) $(LIBS)
$(OBJ_DIR)/bdd
$(OBJ_DIR)/list
$(OBJ_DIR)/error_msg

.PHONY: test_memory_leak
memory_leak:
$(VALGRIND) $(OBJ_DIR)/bdd
$(VALGRIND) $(OBJ_DIR)/list
$(VALGRIND) $(OBJ_DIR)/error_msg

.PHONY: doc
doc:
Expand Down
6 changes: 3 additions & 3 deletions bindings/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ int main()
};

/* Write this into path "/testpath" */
opendal_code code = opendal_operator_blocking_write(op, "/testpath", data);
assert(code == OPENDAL_OK);
opendal_error *error = opendal_operator_blocking_write(op, "/testpath", data);
assert(error == NULL);

/* We can read it out, make sure the data is the same */
opendal_result_read r = opendal_operator_blocking_read(op, "/testpath");
opendal_bytes* read_bytes = r.data;
assert(r.code == OPENDAL_OK);
assert(r.error == NULL);
assert(read_bytes->len == 24);

/* Lets print it out */
Expand Down
1 change: 0 additions & 1 deletion bindings/c/examples/.gitignore

This file was deleted.

13 changes: 8 additions & 5 deletions bindings/c/examples/basicrw.c → bindings/c/examples/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
int main()
{
/* Initialize a operator for "memory" backend, with no options */
const opendal_operator_ptr *op = opendal_operator_new("memory", 0);
assert(op->ptr != NULL);
opendal_result_operator_new result = opendal_operator_new("memory", 0);
assert(result.operator_ptr != NULL);
assert(result.error == NULL);

opendal_operator_ptr* op = result.operator_ptr;

/* Prepare some data to be written */
opendal_bytes data = {
Expand All @@ -34,13 +37,13 @@ int main()
};

/* Write this into path "/testpath" */
opendal_code code = opendal_operator_blocking_write(op, "/testpath", data);
assert(code == OPENDAL_OK);
opendal_error* error = opendal_operator_blocking_write(op, "/testpath", data);
assert(error == NULL);

/* We can read it out, make sure the data is the same */
opendal_result_read r = opendal_operator_blocking_read(op, "/testpath");
opendal_bytes* read_bytes = r.data;
assert(r.code == OPENDAL_OK);
assert(r.error == NULL);
assert(read_bytes->len == 24);

/* Lets print it out */
Expand Down
50 changes: 50 additions & 0 deletions bindings/c/examples/error_handle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "assert.h"
#include "opendal.h"
#include "stdio.h"

// this example shows how to get error message from opendal_error
int main()
{
/* Initialize a operator for "memory" backend, with no options */
opendal_result_operator_new result = opendal_operator_new("memory", 0);
assert(result.operator_ptr != NULL);
assert(result.error == NULL);

opendal_operator_ptr* op = result.operator_ptr;

/* The read is supposed to fail */
opendal_result_read r = opendal_operator_blocking_read(op, "/testpath");
assert(r.error != NULL);
assert(r.error->code == OPENDAL_NOT_FOUND);

/* Lets print the error message out */
struct opendal_bytes* error_msg = &r.error->message;
for (int i = 0; i < error_msg->len; ++i) {
printf("%c", error_msg->data[i]);
}

/* free the error since the error is not NULL */
opendal_error_free(r.error);

/* the operator_ptr is also heap allocated */
opendal_operator_free(op);
}
Loading