-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLMException.cs
More file actions
58 lines (49 loc) · 1.79 KB
/
LLMException.cs
File metadata and controls
58 lines (49 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
namespace YAOLlm;
public class LLMException : Exception
{
public int StatusCode { get; }
public string UserMessage { get; }
public string? Details { get; }
public LLMException(int statusCode, string userMessage, string? details)
: base(userMessage)
{
StatusCode = statusCode;
UserMessage = userMessage;
Details = details;
}
private LLMException(int statusCode, string userMessage, string? details, string fullMessage)
: base(fullMessage)
{
StatusCode = statusCode;
UserMessage = userMessage;
Details = details;
}
public static LLMException CreateWithStatusCode(int statusCode, string? responseBody = null, string? providerName = null)
{
string userMessage = statusCode switch
{
401 => "Invalid API key",
403 => "Access denied",
404 => "API endpoint not found",
429 => "Rate limited - please wait",
>= 500 and <= 504 => "Server error - try again later",
0 => "Connection failed",
_ => "Request failed"
};
string fullMessage = providerName is not null
? $"[{providerName}] {userMessage}"
: userMessage;
return new LLMException(statusCode, userMessage, responseBody, fullMessage);
}
/// <summary>
/// Creates an LLMException with a pre-formatted user message (for response body errors).
/// </summary>
public static LLMException CreateWithMessage(string userMessage, string? providerName = null, string? details = null)
{
string fullMessage = providerName is not null
? $"[{providerName}] {userMessage}"
: userMessage;
return new LLMException(0, userMessage, details, fullMessage);
}
}