Compare commits

...

3 Commits

Author SHA1 Message Date
Erick Friis
07f930b174 merge 2024-01-16 09:59:09 -08:00
Erick Friis
ce62c90f28 merge 2024-01-16 09:56:42 -08:00
Erick Friis
b57df42279 core[patch]: fallbacks error chain 2024-01-16 09:54:16 -08:00

View File

@@ -155,31 +155,32 @@ class RunnableWithFallbacks(RunnableSerializable[Input, Output]):
run_manager = callback_manager.on_chain_start( run_manager = callback_manager.on_chain_start(
dumpd(self), input, name=config.get("run_name") dumpd(self), input, name=config.get("run_name")
) )
first_error = None curr_error: Optional[BaseException] = None
last_error = None
for runnable in self.runnables: for runnable in self.runnables:
try: try:
if self.exception_key and last_error is not None: if self.exception_key and curr_error is not None:
input[self.exception_key] = last_error input[self.exception_key] = curr_error
output = runnable.invoke( output = runnable.invoke(
input, input,
patch_config(config, callbacks=run_manager.get_child()), patch_config(config, callbacks=run_manager.get_child()),
**kwargs, **kwargs,
) )
except self.exceptions_to_handle as e: except self.exceptions_to_handle as e:
if first_error is None: if curr_error is not None:
first_error = e e.__cause__ = curr_error
last_error = e curr_error = e
except BaseException as e: except BaseException as e:
run_manager.on_chain_error(e) if curr_error is not None:
raise e e.__cause__ = curr_error
curr_error = e
break
else: else:
run_manager.on_chain_end(output) run_manager.on_chain_end(output)
return output return output
if first_error is None: if curr_error is None:
raise ValueError("No error stored at end of fallbacks.") raise ValueError("No error stored at end of fallbacks.")
run_manager.on_chain_error(first_error) run_manager.on_chain_error(curr_error)
raise first_error raise curr_error
async def ainvoke( async def ainvoke(
self, self,