← Back to Blog

Logic error to handle the case

Logic error to handle the case BEFORE if(equals(body('contractormarketing-gpt-coverletter')['error'], null), body('contractormarketing-gpt-coverletter')['result'], 'Error occurred') AFTER if(equals(contains(body('contractormarketing-gpt-coverletter'), 'error'), true), 'Error occu

Logic error to handle the case

BEFORE

if(equals(body('contractormarketing-gpt-coverletter')['error'], null), body('contractormarketing-gpt-coverletter')['result'], 'Error occurred')

AFTER

if(equals(contains(body('contractormarketing-gpt-coverletter'), 'error'), true), 'Error occurred', body('contractormarketing-gpt-coverletter')['result'])

Sure, let's break down the differences between the two expressions in your Logic Apps or Power Automate flow:

Before

if(equals(body('contractormarketing-gpt-coverletter')['error'], null), body('contractormarketing-gpt-coverletter')['result'], 'Error occurred')

Explanation:

  • Accessing error Property Directly:

  • The expression body('contractormarketing-gpt-coverletter')['error'] attempts to access the error property directly.

  • Checking if error is null:

  • equals(body('contractormarketing-gpt-coverletter')['error'], null) checks if the error property is null.

  • Return Values:

  • If error is null, it returns body('contractormarketing-gpt-coverletter')['result'].

  • If error is not null (or if it does not exist, leading to an error), it returns 'Error occurred'.

Problem:

  • If the error property does not exist in the body, this expression fails because it tries to access a non-existent property.

After

if(equals(contains(body('contractormarketing-gpt-coverletter'), 'error'), true), 'Error occurred', body('contractormarketing-gpt-coverletter')['result'])

Explanation:

  • Checking Existence of error Property:

  • contains(body('contractormarketing-gpt-coverletter'), 'error') checks if the error property exists in the body.

  • Evaluating Boolean Condition:

  • equals(contains(body('contractormarketing-gpt-coverletter'), 'error'), true) evaluates to true if the error property exists, otherwise false.

  • Return Values:

  • If contains evaluates to true (i.e., error property exists), it returns 'Error occurred'.

  • If contains evaluates to false (i.e., error property does not exist), it returns body('contractormarketing-gpt-coverletter')['result'].

Improvement:

  • The contains function safely checks for the existence of the error property without attempting to access it directly.

  • This avoids the risk of trying to access a property that might not exist, preventing the error you encountered.

Summary of Differences

  • Error Handling:

  • Before: Directly accesses the error property, leading to an error if it does not exist.

  • After: Checks for the existence of the error property before accessing it, preventing errors.

  • Condition Logic:

  • Before: Returns body('contractormarketing-gpt-coverletter')['result'] if error is null, otherwise 'Error occurred'.

  • After: Returns 'Error occurred' if error exists, otherwise body('contractormarketing-gpt-coverletter')['result'].

  • Robustness:

  • Before: Less robust due to potential access of non-existent property.

  • After: More robust due to the preliminary check using contains.

In conclusion, the "After" version is designed to handle cases where the error property might not be present, making the expression more robust and preventing runtime errors.

if(equals(contains(body('contractormarketing-gpt-coverletter'), 'error'), true), 'Error occurred', body('contractormarketing-gpt-coverletter')['result'])

logic error to 1

ActionFailed. An action failed. No dependent actions succeeded.

logic error to 2

logic error to 3


Imported from rifaterdemsahin.com · 2024