Added exception handling

This commit is contained in:
Thorsten Sommer 2025-02-11 13:43:16 +01:00
parent 57af2080f5
commit 1900cd2028
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -11,9 +11,11 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
#region Implementation of IERIClient
public async Task<APIResponse<List<AuthScheme>>> GetAuthMethodsAsync(CancellationToken cancellationToken = default)
{
try
{
using var response = await this.httpClient.GetAsync("/auth/methods", cancellationToken);
if(!response.IsSuccessStatusCode)
if (!response.IsSuccessStatusCode)
{
return new()
{
@ -23,7 +25,7 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
}
var authMethods = await response.Content.ReadFromJsonAsync<List<AuthScheme>>(JSON_OPTIONS, cancellationToken);
if(authMethods is null)
if (authMethods is null)
{
return new()
{
@ -38,8 +40,27 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
Data = authMethods
};
}
catch (TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to retrieve the authentication methods: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to retrieve the authentication methods due to an exception: {e.Message}"
};
}
}
public async Task<APIResponse<AuthResponse>> AuthenticateAsync(IERIDataSource dataSource, RustService rustService, CancellationToken cancellationToken = default)
{
try
{
var authMethod = dataSource.AuthMethod;
var username = dataSource.Username;
@ -176,8 +197,27 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
};
}
}
catch(TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to authenticate with the ERI server: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to authenticate with the ERI server due to an exception: {e.Message}"
};
}
}
public async Task<APIResponse<DataSourceInfo>> GetDataSourceInfoAsync(CancellationToken cancellationToken = default)
{
try
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/dataSource");
request.Headers.Add("token", this.securityToken);
@ -208,8 +248,27 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
Data = dataSourceInfo
};
}
catch(TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to retrieve the data source information: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to retrieve the data source information due to an exception: {e.Message}"
};
}
}
public async Task<APIResponse<List<EmbeddingInfo>>> GetEmbeddingInfoAsync(CancellationToken cancellationToken = default)
{
try
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/embedding/info");
request.Headers.Add("token", this.securityToken);
@ -240,8 +299,27 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
Data = embeddingInfo
};
}
catch(TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to retrieve the embedding information: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to retrieve the embedding information due to an exception: {e.Message}"
};
}
}
public async Task<APIResponse<List<RetrievalInfo>>> GetRetrievalInfoAsync(CancellationToken cancellationToken = default)
{
try
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/retrieval/info");
request.Headers.Add("token", this.securityToken);
@ -272,8 +350,27 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
Data = retrievalInfo
};
}
catch(TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to retrieve the retrieval information: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to retrieve the retrieval information due to an exception: {e.Message}"
};
}
}
public async Task<APIResponse<List<Context>>> ExecuteRetrievalAsync(RetrievalRequest request, CancellationToken cancellationToken = default)
{
try
{
using var requestMessage = new HttpRequestMessage(HttpMethod.Post, "/retrieval");
requestMessage.Headers.Add("token", this.securityToken);
@ -307,8 +404,27 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
Data = contexts
};
}
catch(TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to execute the retrieval request: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to execute the retrieval request due to an exception: {e.Message}"
};
}
}
public async Task<APIResponse<SecurityRequirements>> GetSecurityRequirementsAsync(CancellationToken cancellationToken = default)
{
try
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/security/requirements");
request.Headers.Add("token", this.securityToken);
@ -339,6 +455,23 @@ public class ERIClientV1(string baseAddress) : ERIClientBase(baseAddress), IERIC
Data = securityRequirements
};
}
catch(TaskCanceledException)
{
return new()
{
Successful = false,
Message = "Failed to retrieve the security requirements: the request was canceled either by the user or due to a timeout."
};
}
catch (Exception e)
{
return new()
{
Successful = false,
Message = $"Failed to retrieve the security requirements due to an exception: {e.Message}"
};
}
}
#endregion
}