Skip to main content

How to Catch Building Artifacts Errors

This guide shows you how to properly handle artifact building errors in BXL using the ctx.lazy.build_artifact API.

Prerequisites

Read How to Handle Errors first to first to understand BXL's error handling patterns.

Best Practices

1. Prepare the artifacts to be built

2. Use ctx.lazy.build_artifact api

lazy_built = ctx.lazy.build_artifact(artifact)
# catch error and resolve Lazy object
result = lazy_built.catch().resolve()

For how to parallel building a list of artifacts please refer here for more details

3. Call ctx.output.ensure/ensure_multiple to materialize artifacts

if result.is_ok():
artifact = result.unwrap()
ctx.output.ensure(artifact)
else:
error = result.unwrap_err()
print(error)

Important Limitations

You cannot use this API for artifacts declared in BXL.

Examples

Examples can be found at tests/core/bxl/test_lazy_build_artifact_data/lazy_build_artifact.bxl of buck2 repo folder.

Run such command at tests/core/bxl/test_lazy_build_artifact_data to run the example bxl script

buck2 bxl lazy_build_artifact.bxl:build_artifact
buck2 bxl lazy_build_artifact.bxl:build_artifact_fail