Skip to content

Commit ae132d1

Browse files
Removed magic string for property names from exceptions
1 parent 6691350 commit ae132d1

File tree

7 files changed

+22
-15
lines changed

7 files changed

+22
-15
lines changed

RecipeSocialMediaAPI.Application.Tests.Unit/Exceptions/HandleAlreadyInUseExceptionTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void HandleAlreadyInUseException_IsCorrectlySerializedAndDeserialized()
1717
var handle = "test_handler";
1818
HandleAlreadyInUseException exception = new(handle);
1919

20-
CustomExceptionSerializationData serializedData = new(exception.Message, exception.HResult, exception.Source, exception.StackTrace, handle);
20+
CustomExceptionSerializationData serializedData = new(exception.Message, exception.HResult, exception.Source, exception.StackTrace, exception.Handle);
2121

2222
// When
2323
var json = JsonSerializer.Serialize(serializedData);

RecipeSocialMediaAPI.Application/Exceptions/EmailAlreadyInUseException.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace RecipeSocialMediaAPI.Application.Exceptions;
55
[Serializable]
66
public class EmailAlreadyInUseException : Exception
77
{
8+
private const string EMAIL_PROPERTY_NAME = "Email";
89
public string Email { get; }
910

1011
public EmailAlreadyInUseException(string email)
@@ -14,12 +15,12 @@ public EmailAlreadyInUseException(string email)
1415

1516
protected EmailAlreadyInUseException(SerializationInfo info, StreamingContext context) : base(info, context)
1617
{
17-
Email = info.GetString("Email") ?? string.Empty;
18+
Email = info.GetString(EMAIL_PROPERTY_NAME) ?? string.Empty;
1819
}
1920

2021
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2122
{
2223
base.GetObjectData(info, context);
23-
info.AddValue("Email", Email);
24+
info.AddValue(EMAIL_PROPERTY_NAME, Email);
2425
}
2526
}

RecipeSocialMediaAPI.Application/Exceptions/HandleAlreadyInUseException.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@ namespace RecipeSocialMediaAPI.Application.Exceptions;
55
[Serializable]
66
public class HandleAlreadyInUseException : Exception
77
{
8+
private const string HANDLE_PROPERTY_NAME = "Handle";
89
public string Handle { get; }
910

10-
public HandleAlreadyInUseException(string handler)
11+
public HandleAlreadyInUseException(string handle)
1112
{
12-
Handle = handler;
13+
Handle = handle;
1314
}
1415

1516
protected HandleAlreadyInUseException(SerializationInfo info, StreamingContext context) : base(info, context)
1617
{
17-
Handle = info.GetString("Handler") ?? string.Empty;
18+
Handle = info.GetString(HANDLE_PROPERTY_NAME) ?? string.Empty;
1819
}
1920

2021
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2122
{
2223
base.GetObjectData(info, context);
23-
info.AddValue("Handle", Handle);
24+
info.AddValue(HANDLE_PROPERTY_NAME, Handle);
2425
}
2526
}

RecipeSocialMediaAPI.Application/Exceptions/InvalidUserRoleException.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ namespace RecipeSocialMediaAPI.Application.Exceptions;
55
[Serializable]
66
public class InvalidUserRoleException : Exception
77
{
8+
private const string INVALID_ROLE_PROPERTY_NAME = "InvalidRole";
89
public string InvalidRole { get; }
10+
911
public InvalidUserRoleException(string role) : base($"Invalid user role {role}")
1012
{
1113
InvalidRole = role;
1214
}
1315

1416
protected InvalidUserRoleException(SerializationInfo info, StreamingContext context) : base(info, context)
1517
{
16-
InvalidRole = info.GetString("InvalidRole") ?? string.Empty;
18+
InvalidRole = info.GetString(INVALID_ROLE_PROPERTY_NAME) ?? string.Empty;
1719
}
1820

1921
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2022
{
2123
base.GetObjectData(info, context);
22-
info.AddValue("InvalidRole", InvalidRole);
24+
info.AddValue(INVALID_ROLE_PROPERTY_NAME, InvalidRole);
2325
}
2426
}

RecipeSocialMediaAPI.Application/Exceptions/UnsupportedConnectionStatusException.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace RecipeSocialMediaAPI.Application.Exceptions;
66
[Serializable]
77
public class UnsupportedConnectionStatusException : Exception
88
{
9+
private const string UNSUPPORTED_STATUS_PROPERTY_NAME = "UnsupportedStatus";
910
public string UnsupportedStatus { get; }
1011

1112
public UnsupportedConnectionStatusException(string connectionStatus) : base($"Could not map {connectionStatus} to {typeof(ConnectionStatus)}")
@@ -15,12 +16,12 @@ public UnsupportedConnectionStatusException(string connectionStatus) : base($"Co
1516

1617
protected UnsupportedConnectionStatusException(SerializationInfo info, StreamingContext context) : base(info, context)
1718
{
18-
UnsupportedStatus = info.GetString("UnsupportedStatus") ?? string.Empty;
19+
UnsupportedStatus = info.GetString(UNSUPPORTED_STATUS_PROPERTY_NAME) ?? string.Empty;
1920
}
2021

2122
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2223
{
2324
base.GetObjectData(info, context);
24-
info.AddValue("UnsupportedStatus", UnsupportedStatus);
25+
info.AddValue(UNSUPPORTED_STATUS_PROPERTY_NAME, UnsupportedStatus);
2526
}
2627
}

RecipeSocialMediaAPI.Application/Exceptions/UsernameAlreadyInUseException.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace RecipeSocialMediaAPI.Application.Exceptions;
55
[Serializable]
66
public class UsernameAlreadyInUseException : Exception
77
{
8+
private const string USERNAME_PROPERTY_NAME = "Username";
89
public string Username { get; }
910

1011
public UsernameAlreadyInUseException(string username)
@@ -14,12 +15,12 @@ public UsernameAlreadyInUseException(string username)
1415

1516
protected UsernameAlreadyInUseException(SerializationInfo info, StreamingContext context) : base(info, context)
1617
{
17-
Username = info.GetString("Username") ?? string.Empty;
18+
Username = info.GetString(USERNAME_PROPERTY_NAME) ?? string.Empty;
1819
}
1920

2021
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2122
{
2223
base.GetObjectData(info, context);
23-
info.AddValue("Username", Username);
24+
info.AddValue(USERNAME_PROPERTY_NAME, Username);
2425
}
2526
}

RecipeSocialMediaAPI.Infrastructure/Exceptions/DocumentAlreadyExistsException.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace RecipeSocialMediaAPI.Infrastructure.Exceptions;
66
[Serializable]
77
public class DocumentAlreadyExistsException<TDocument> : Exception where TDocument : MongoDocument
88
{
9+
private const string DOCUMENT_PROPERTY_NAME = "Document";
910
public TDocument Document { get; }
1011

1112
public DocumentAlreadyExistsException(TDocument document) : base($"{document.GetType()} already exists with id {document.Id}")
@@ -15,12 +16,12 @@ public DocumentAlreadyExistsException(TDocument document) : base($"{document.Get
1516

1617
protected DocumentAlreadyExistsException(SerializationInfo info, StreamingContext context) : base(info, context)
1718
{
18-
Document = info.GetValue("Document", typeof(TDocument)) as TDocument ?? default!;
19+
Document = info.GetValue(DOCUMENT_PROPERTY_NAME, typeof(TDocument)) as TDocument ?? default!;
1920
}
2021

2122
public override void GetObjectData(SerializationInfo info, StreamingContext context)
2223
{
2324
base.GetObjectData(info, context);
24-
info.AddValue("Document", Document);
25+
info.AddValue(DOCUMENT_PROPERTY_NAME, Document);
2526
}
2627
}

0 commit comments

Comments
 (0)