Repository navigation
Issue #281 Fix JSON error handling #448
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
Closed
KatharinaJacoby
wants to merge
8
commits into
FailproofAI:main
from
KatharinaJacoby:docs/jsondecodeerror-example
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5373368
Create index.md
KatharinaJacoby f2b831f
Index
KatharinaJacoby 1467298
Delete docs/docs/exosphere/index.md
KatharinaJacoby e762379
Index
KatharinaJacoby ae70981
add missing BaseNode import to index.md example
KatharinaJacoby eaf5c0d
Update Index
KatharinaJacoby 7d6376c
Update Index
KatharinaJacoby d79c622
Update Index
KatharinaJacoby File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Exosphere Example: DataProcessorNode | ||
|
|
||
| ```python | ||
| import json | ||
| import logging | ||
| from enum import Enum | ||
| from pydantic import BaseModel | ||
| from exospherehost import BaseNode | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| class Operation(str, Enum): | ||
| """Supported operations the node can perform.""" | ||
| TRANSFORM = "transform" | ||
| PASSTHROUGH = "passthrough" | ||
|
|
||
| class DataProcessorNode(BaseNode): | ||
| """Parse JSON input and apply a simple operation, returning a JSON string result.""" | ||
|
|
||
| class Inputs(BaseModel): | ||
| """Inputs for DataProcessorNode. | ||
|
|
||
| data: JSON-encoded string payload to process. | ||
| operation: one of Operation (validated). | ||
| """ | ||
| data: str | ||
| operation: Operation | ||
|
|
||
| class Outputs(BaseModel): | ||
| """Outputs emitted by DataProcessorNode. | ||
|
|
||
| result: JSON-encoded string payload after processing. | ||
| status: 'success' or an error message. | ||
| """ | ||
| result: str | ||
| status: str | ||
|
|
||
| async def execute(self) -> Outputs: | ||
| """Execute the node: parse input JSON and perform the requested operation.""" | ||
| # Parse the input data | ||
| try: | ||
| data = json.loads(self.inputs.data) | ||
| except json.JSONDecodeError as e: | ||
| # Log for observability and return specific, helpful feedback | ||
| logger.warning("Invalid JSON in DataProcessorNode.inputs.data", exc_info=True) | ||
| return self.Outputs( | ||
| result="", | ||
| status=f"error: invalid json (line {getattr(e, 'lineno', '?')}, column {getattr(e, 'colno', '?')})" | ||
| ) | ||
|
|
||
| # Process based on validated operation | ||
| if self.inputs.operation is Operation.TRANSFORM: | ||
| result_obj = {"transformed": data, "processed": True} | ||
| else: # Operation.PASSTHROUGH | ||
| result_obj = {"original": data, "processed": False} | ||
|
|
||
| # Outputs.result is defined as a string; return JSON-encoded result for consistency | ||
| try: | ||
| result_json = json.dumps(result_obj) | ||
| except (TypeError, ValueError) as e: | ||
| logger.warning("Failed to serialize result", exc_info=True) | ||
| return self.Outputs( | ||
| result="", | ||
| status=f"error: serialization failed - {e}" | ||
| ) | ||
|
|
||
| return self.Outputs(result=result_json, status="success") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.