proxygen
set_error_2.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
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 <algorithm>
17 #include <cassert>
18 #include <iostream>
19 #include <vector>
20 
27 
28 using namespace folly::pushmi::aliases;
29 
30 // concat not yet implemented
31 template <class T, class E = std::exception_ptr>
32 auto concat = [](auto in) {
33  return mi::make_single_sender([in](auto out) mutable {
34  mi::submit(in, mi::make_receiver(out, [](auto out, auto v) {
36  }));
37  });
38 };
39 
40 int main() {
41  auto stop_abort = mi::on_error([](auto) noexcept {});
42  // support all error value types
43 
44  op::error(std::exception_ptr{}) | op::submit(stop_abort);
45 
46  op::error(std::errc::argument_list_too_long) | op::submit(stop_abort);
47 
48  // transform an error
49 
50  op::error(std::errc::argument_list_too_long) | op::switch_on_error([
51  ](auto) noexcept { return op::error(std::exception_ptr{}); }) |
52  op::submit(stop_abort);
53 
54  // use default value if an error occurs
55 
56  op::just(42) |
57  op::switch_on_error([](auto) noexcept { return op::just(0); }) |
58  op::submit();
59 
60  // suppress if an error occurs
61 
62  op::error(std::errc::argument_list_too_long) |
63  op::switch_on_error([](auto) noexcept { return op::empty(); }) |
64  op::submit();
65 
66  // abort if an error occurs
67 
68  op::just(42) | op::no_fail() | op::submit();
69 
70  // transform value to error_
71 
72  op::just(42) | op::transform([](auto v) {
74  if (v < 40) {
75  return r_t{op::error<int>(std::exception_ptr{})};
76  } else {
77  return r_t{op::just(v)};
78  }
79  }) | concat<int> |
80  op::submit();
81 
82  // retry on error
83 
84  // http.get(ex) |
85  // op::timeout(ex, 1s) |
86  // op::switch_on_error([](auto e) noexcept { return op::timer(ex, 1s); }) |
87  // op::repeat() |
88  // op::timeout(ex, 10s) |
89  // op::submit();
90 
91  std::cout << "OK" << std::endl;
92 }
PUSHMI_INLINE_VAR constexpr detail::switch_on_error_fn switch_on_error
auto on_error(Fns...fns) -> on_error_fn< Fns... >
Definition: boosters.h:273
requires E e noexcept(noexcept(s.error(std::move(e))))
PUSHMI_INLINE_VAR constexpr detail::transform_fn transform
Definition: transform.h:158
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
int main()
Definition: set_error_2.cpp:40
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::just_fn just
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::make_receiver_fn make_receiver
auto concat
Definition: set_error_2.cpp:32
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::make_single_sender_fn make_single_sender
PUSHMI_INLINE_VAR constexpr detail::no_fail_fn no_fail
Definition: no_fail.h:59