Skip to content

Commit

Permalink
Bugfix/do now swallow exceptions during verify (#141)
Browse files Browse the repository at this point in the history
* Remove unused parameters from constructor

Eliminated the utcDataService and signatureService parameters from the SecuredOnboardingService constructor as they were no longer in use. This cleanup helps streamline the code and improve readability.

* Rename exception base class to include verification logic

Renamed OnboardRevokeExceptionBase to OnboardRevokeOrVerifyExceptionBase to better reflect its purpose. Updated all derived exception classes to inherit from the new base class.

* Simplify error handling in SecuredOnboardingService

Replaced the detailed switch-case error handling with a more concise deserialization of error responses using OnboardErrorResponse class. This change improves readability and maintainability by centralizing error message construction within the OnboardException.

---------

Co-authored-by: Frank-Wiebeler <[email protected]>
  • Loading branch information
saschadoemer and Frank-Wiebeler authored Nov 14, 2024
1 parent e1b8312 commit 6a0589e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Agrirouter.Api.Exception
/// Will be thrown if the onboarding process is not successful.
/// </summary>
[Serializable]
public class OnboardException : OnboardRevokeExceptionBase {
public class OnboardException : OnboardRevokeOrVerifyExceptionBase {
/// <summary>
/// Constructor.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Agrirouter.Api.Exception
/// Base exception class for errors during onboarding and revoking processes.
/// </summary>
[Serializable]
public class OnboardRevokeExceptionBase : System.Exception
public class OnboardRevokeOrVerifyExceptionBase : System.Exception
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="statusCode">-</param>
/// <param name="onboardError">-</param>
public OnboardRevokeExceptionBase(HttpStatusCode statusCode, OnboardError onboardError): base(onboardError.Message)
public OnboardRevokeOrVerifyExceptionBase(HttpStatusCode statusCode, OnboardError onboardError): base(onboardError.Message)
{
StatusCode = statusCode;
Error = onboardError;
Expand All @@ -26,7 +26,7 @@ public OnboardRevokeExceptionBase(HttpStatusCode statusCode, OnboardError onboar
/// </summary>
/// <param name="statusCode">-</param>
/// <param name="message">-</param>
public OnboardRevokeExceptionBase(HttpStatusCode statusCode, string message): base(message)
public OnboardRevokeOrVerifyExceptionBase(HttpStatusCode statusCode, string message): base(message)
{
StatusCode = statusCode;
Error = new OnboardError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Agrirouter.Api.Exception
/// Will be thrown if the revoking process is not successful.
/// </summary>
[Serializable]
public class RevokeException : OnboardRevokeExceptionBase {
public class RevokeException : OnboardRevokeOrVerifyExceptionBase {
/// <summary>
/// Constructor.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ namespace Agrirouter.Api.Exception
/// Will be thrown if the verification process is not successful.
/// </summary>
[Serializable]
public class VerificationException : System.Exception
public class VerificationException : OnboardRevokeOrVerifyExceptionBase
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="statusCode">-</param>
/// <param name="onboardError">-</param>
public VerificationException(HttpStatusCode statusCode, OnboardError onboardError) : base(statusCode,
onboardError)
{
}

/// <summary>
/// Constructor with error message only.
/// </summary>
/// <param name="statusCode">-</param>
/// <param name="message">-</param>
public VerificationException(string message) : base(message)
public VerificationException(HttpStatusCode statusCode, string message) : base(statusCode, message)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ public class SecuredOnboardingService
/// Constructor.
/// </summary>
/// <param name="environment">The current environment.</param>
/// <param name="utcDataService">The UTC data service.</param>
/// <param name="signatureService">The signature service.</param>
/// <param name="httpClient">The current HTTP client.</param>
public SecuredOnboardingService(Environment environment, HttpClient httpClient)
{
Expand Down Expand Up @@ -168,17 +166,8 @@ public VerificationResponse Verify(VerificationParameters verificationParameters

if (!httpResponseMessage.IsSuccessStatusCode)
{
switch (httpResponseMessage.StatusCode)
{
case HttpStatusCode.BadRequest:
throw new VerificationException(
"There was an error within the request. Please check the request parameters.");
case HttpStatusCode.Unauthorized:
throw new VerificationException("The registration code is invalid.");
default:
throw new VerificationException("The request failed, the error is unknown. The error code is " +
httpResponseMessage.StatusCode + ".");
}
var onboardErrorResponse = JsonConvert.DeserializeObject<OnboardErrorResponse>(result);
throw new OnboardException(httpResponseMessage.StatusCode, onboardErrorResponse.OnboardError);
}

var response = JsonConvert.DeserializeObject(result, typeof(VerificationResponse));
Expand Down Expand Up @@ -225,17 +214,8 @@ public async Task<VerificationResponse> VerifyAsync(VerificationParameters verif

if (!httpResponseMessage.IsSuccessStatusCode)
{
switch (httpResponseMessage.StatusCode)
{
case HttpStatusCode.BadRequest:
throw new VerificationException(
"There was an error within the request. Please check the request parameters.");
case HttpStatusCode.Unauthorized:
throw new VerificationException("The registration code is invalid.");
default:
throw new VerificationException("The request failed, the error is unknown. The error code is " +
httpResponseMessage.StatusCode + ".");
}
var onboardErrorResponse = JsonConvert.DeserializeObject<OnboardErrorResponse>(result);
throw new OnboardException(httpResponseMessage.StatusCode, onboardErrorResponse.OnboardError);
}

var response = JsonConvert.DeserializeObject<VerificationResponse>(result);
Expand Down

0 comments on commit 6a0589e

Please sign in to comment.