Skip to content
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

Add auth header error handling #250

Merged
merged 6 commits into from
Dec 17, 2024
Merged

Conversation

alecgeatches
Copy link
Contributor

@alecgeatches alecgeatches commented Dec 16, 2024

Allow get_request_headers() implementors to return a WP_Error to error-out early. Useful for data sources with complex authentication requirements that may know a request will fail during the auth stage. This will allow for more targeted error messages on authentication failure, and avoid making requests to APIs that will be known to fail.

Testing

  1. While using the trunk branch, go to Admin -> Settings -> Remote Data Blocks, and create a new "Salesforce B2C" data source.

  2. Delete the last character of the Client ID to create an invalid source.

  3. Go to a new post and add the generated Salesforce B2C block.

  4. Open network tools to view the results of requests.

  5. In the Salesforce B2C block, click the "Search for an item" button and type some text into the search bar (e.g. shirt).

  6. View the network panel, and see the failed request. See a non-specific error message indicating an unauthorized request:

    Screenshot 2024-12-16 at 1 04 30 PM

  7. Switch to the add/auth-header-error-handling branch, and type into the search bar again.

  8. View the network panel, and see the failed request. The request should now contain a authorization-specific error message:
    Screenshot 2024-12-16 at 1 04 50 PM

Notes

  • We aren't yet showing errors from the API response on failure, yet.

@alecgeatches alecgeatches self-assigned this Dec 16, 2024
@@ -27,9 +27,6 @@ abstract public function get_display_name(): string;

abstract public function get_endpoint(): string;

/**
* @inheritDoc
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note these @inheritDocs were removed due to a failed phpcs check:

$ composer run phpcs

FILE: tests/inc/Mocks/MockDataSource.php
--------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------------------
 36 | ERROR | [x] Useless documentation comment with @inheritDoc. (SlevomatCodingStandard.Commenting.UselessInheritDocComment.UselessInheritDocComment)
--------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: inc/ExampleApi/Queries/ExampleApiDataSource.php
--------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------------------
 25 | ERROR | [x] Useless documentation comment with @inheritDoc. (SlevomatCodingStandard.Commenting.UselessInheritDocComment.UselessInheritDocComment)
--------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: inc/Config/DataSource/HttpDataSource.php
--------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------------------------------
 30 | ERROR | [x] Useless documentation comment with @inheritDoc. (SlevomatCodingStandard.Commenting.UselessInheritDocComment.UselessInheritDocComment)
--------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------------------------------

@maxschmeling maxschmeling self-requested a review December 17, 2024 14:27
@alecgeatches alecgeatches merged commit 2534f64 into trunk Dec 17, 2024
11 checks passed
@alecgeatches alecgeatches deleted the add/auth-header-error-handling branch December 17, 2024 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants