Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit ad91d11

Browse files
committed
Retrieve message from error JSON boyd
1 parent 3883138 commit ad91d11

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/AssemblyAI/Core/Public/ApiException.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
using System.Text.Json;
2+
using AssemblyAI.Core;
3+
14
namespace AssemblyAI;
25

36
/// <summary>
47
/// This exception type will be thrown for any non-2XX API responses.
58
/// </summary>
69
public class ApiException(string message, int statusCode, object body)
7-
: AssemblyAIException(body is Error error ? error.Error_ : message)
10+
: AssemblyAIException(GetMessageFromJsonBody(message, body))
811
{
912
/// <summary>
1013
/// The error code of the response that triggered the exception.
@@ -15,4 +18,18 @@ public class ApiException(string message, int statusCode, object body)
1518
/// The body of the response that triggered the exception.
1619
/// </summary>
1720
internal object Body { get; } = body;
18-
}
21+
22+
private static string GetMessageFromJsonBody(string message, object body)
23+
{
24+
if (body is not string stringBody) return message;
25+
try
26+
{
27+
var errorObject = JsonUtils.Deserialize<Error>(stringBody);
28+
return errorObject.Error_;
29+
}
30+
catch (JsonException)
31+
{
32+
return stringBody;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)