fix: show actionable Business Policy setup instructions instead of raw JSON on 400 error

This commit is contained in:
2026-04-16 12:12:43 +01:00
parent 135fd07f54
commit 554a280caa

View File

@@ -1,4 +1,4 @@
using System.Net.Http;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using EbayListingTool.Models;
@@ -174,6 +174,35 @@ public class EbayListingService
}
}
/// <summary>Parses an eBay error JSON body into a user-friendly message.</summary>
private static string ExtractEbayError(string json, string policyType)
{
try
{
var errors = JObject.Parse(json)["errors"] as JArray;
var first = errors?.FirstOrDefault() as JObject;
if (first != null)
{
var errorId = first["errorId"]?.Value<int>() ?? 0;
var longMsg = first["longMessage"]?.ToString() ?? first["message"]?.ToString() ?? "";
// 20403 = account not opted in to Business Policies
if (errorId == 20403 || longMsg.Contains("not eligible for Business Policy"))
return $"Your eBay account is not set up for Business Policies, which are required to post listings via the API.\n\n" +
$"To fix this:\n" +
$"1. Log in to the eBay Seller Hub (or sandbox Seller Hub)\n" +
$"2. Go to Account \u2192 Business policies\n" +
$"3. Create at least one Postage, Payment and Returns policy\n\n" +
$"Once done, click Post to eBay again.";
if (!string.IsNullOrWhiteSpace(longMsg))
return $"eBay {policyType} error: {longMsg}";
}
}
catch { }
return $"Could not fetch {policyType} from eBay. Please check your account settings.";
}
private async Task CreateMerchantLocationAsync(string token, string postcode)
{
if (string.IsNullOrWhiteSpace(postcode))