EasyPost/easypost-php

[Bug]: Error when creating standalone insurance

abishekrsrikaanth opened this issue · 4 comments

Software Version

6.6.0

Language Version

8.1

Operating System

Mac OS, Ubuntu 22.04

What happened?

I tried to create a standalone insurance using both the Parent account and a child account and I get this error.

EasyPost\Exception\Api\NotFoundException  The requested resource could not be found.

What was expected?

A standalone insurance was to be created and the response with the insurance object was to be returned.

Sample Code

Here is the code I am using

use EasyPost\EasyPostClient;
use EzeeSpace\Core\Models\ShopifyUser;

$user = ShopifyUser::find(1);

$coreUser = $user->tenant->user;

$epCredential = $coreUser->easyPostCredential;

$easypostClient = new EasyPostClient("PARENT_API_KEY_OR_CHILD_USER_API_KEY");

$insuranceObj = $easypostClient->insurance->create([
    'from_address' => ['id' => 'adr_716d66fafc9911eca344ac1f6bc7b362'],
    'to_address' => ['id' => 'adr_bc6b7716eec211eda12dac1f6bc7b362'],
    'amount' => 300,
    'carrier' => 'USPS',
    'tracking_code' => 'EZ1000000001',
]);

Relevant logs

No response

Hello, thanks for reaching out! Just wanted to let you know we are looking into your issue currently, we'll keep you updated.

Hello @abishekrsrikaanth, wanted to follow up with my findings.

I took your code snippet and ran it with a VCR to capture and audit the request-response pairs, and was able to recreate your error. Initially I was concerned there was an issue with it perhaps hitting a misspelled endpoint, but I've confirmed that the library is calling the API correctly. The exception is being thrown because the server is reporting that the provided information is invalid.

Specifically, this response:
image

According to the body of the response, the 404 is because the address ID is invalid. Now, for me, it's invalid at least because I am using a different account (and therefore don't have access to the address associated with that ID). For you, the 404 might be due to another validation issue.

If you wrap your code in a try-catch, you can analyze the details of the raised exception by pretty-printing it, like so:

try {
    $insuranceObj = self::$client->insurance->create([
        'from_address' => ['id' => 'adr_716d66fafc9911eca344ac1f6bc7b362'],
        'to_address' => ['id' => 'adr_bc6b7716eec211eda12dac1f6bc7b362'],
        'amount' => 300,
        'carrier' => 'USPS',
        'tracking_code' => 'EZ1000000001',
    ]);
} catch (\EasyPost\Exception\Api\ApiException $error) {
    $error->prettyPrint();
}

Which will output like so:
image

You can also access different aspects of the API exception, including ->getHttpStatus(), ->code (the EasyPost error code, e.g. MISSING_PARAMETER), ->getMessage(), and ->errors (an array of individual errors if multiple things are wrong with the request)

Since this is not an issue with our client library, I'm going to close this ticket and direct you to our support team (support@easypost.com) for further assistance. Feel free to comment on this issue or open a new one if anything else comes up.

Thanks for using EasyPost!

@nwithan8 thank you this information really helps and thanks for the additional info about prettyPrint. I do have one question about the resource associated with the account.

  1. Are resources created on parent account accessible by the child accounts?
  2. Are resources created on one child account accessible by another child account or the parent account?

I used the prettyPrint method and I do get the same error message as you that is more clear. But the above questions may help figure this out more and also our implementation.

@abishekrsrikaanth Our support team would be better capable to answer those types of questions.