Skip to content

Commit f598479

Browse files
committed
Throwning runtime error in case of an Abort(). Much easier for the client side.
1 parent 99826ed commit f598479

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ More info see: [Doxygen Docs - Client Class](https://vasild.github.io/cpp-ipfs-h
133133
134134
The client constructor and destructor are not thread safe. However, all the API IPFS calls are **thread safe**!
135135
136+
*Note:* A runtime error will be thrown on the request call (in this example the `FilesGet()`) when you call the `Abort()` method, allowing you to stop your code execution inside the thread.
137+
136138
An example of using a thread together with the IPFS Client:
137139
138140
```cpp

examples/threading_example.cc

+4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ void startThread() {
4646
// File should not exists, takes forever (until time-out)
4747
client.FilesGet("QmZp1rrtGTictR2rpNcx4673R7qU9Jdr9DQ6Z7F6Wgo2bQ",
4848
&contents);
49+
50+
// Code below will never be reached, since we abort the request
51+
std::cout << "Output: " << contents.str() << std::endl;
4952
} catch (const std::runtime_error& e) {
53+
// Run-time error will be thrown because of the aborting request.
5054
std::cerr << "Error: " << e.what() << std::endl;
5155
}
5256
});

src/http/transport-curl.cc

+5
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,11 @@ void TransportCurl::Perform(const std::string& url, std::iostream* response) {
309309
throw std::runtime_error(error);
310310
}
311311
}
312+
} else {
313+
/* Throw runtime error if the request was aborted (atomic bool is false)
314+
* This is useful for the client-side in order to
315+
* stop executing remaining code when a Abort() was triggered. */
316+
throw std::runtime_error("Request was aborted");
312317
}
313318
}
314319

test/test_threading.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ int main(int, char**) {
3939
client.FilesGet("QmZp1rrtGTictR2rpNcx4673R7qU9Jdr9DQ6Z7F6Wgo2bQ",
4040
&contents);
4141
} catch (const std::runtime_error& e) {
42-
std::cerr << "Error: " << e.what() << std::endl;
42+
std::cerr << "Expected error: " << e.what() << std::endl;
4343
}
4444
});
4545

0 commit comments

Comments
 (0)