-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errorhandling)! enhance exceptions with details for errortype, e…
…rrorcode and parameters (#315) add new exceptions for errortype, errorcode and parameters ------- Refs: CPLP-3090 Reviewed-By: Phil Schneider <[email protected]>
- Loading branch information
1 parent
5d1b233
commit 897f8bc
Showing
19 changed files
with
441 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...n/Administration.Service/ErrorHandling/AdministrationRegistrationErrorMessageContainer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library; | ||
using System.Collections.Immutable; | ||
|
||
namespace Org.Eclipse.TractusX.Portal.Backend.Administration.ErrorHandling; | ||
|
||
public class AdministrationRegistrationErrorMessageContainer : IErrorMessageContainer | ||
{ | ||
private static readonly IReadOnlyDictionary<int, string> _messageContainer = new Dictionary<AdministrationRegistrationErrors, string> { | ||
{ AdministrationRegistrationErrors.APPLICATION_NOT_FOUND, "application {applicationId} does not exist" } | ||
}.ToImmutableDictionary(x => (int)x.Key, x => x.Value); | ||
|
||
public Type Type { get => typeof(AdministrationRegistrationErrors); } | ||
public IReadOnlyDictionary<int, string> MessageContainer { get => _messageContainer; } | ||
} | ||
|
||
public enum AdministrationRegistrationErrors | ||
{ | ||
APPLICATION_NOT_FOUND | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/framework/Framework.ErrorHandling.Library/DetailException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
using Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling; | ||
|
||
[Serializable] | ||
public abstract class DetailException : Exception | ||
{ | ||
private static readonly Regex _templateMatcherExpression = new Regex(@"\{(\w+)\}", RegexOptions.None, TimeSpan.FromSeconds(1)); // to replace any text surrounded by { and } | ||
private enum NoDetailsErrorType | ||
{ | ||
NONE = 0 | ||
} | ||
|
||
protected DetailException() : base() { } | ||
protected DetailException(string message) : base(message) { } | ||
protected DetailException(string message, Exception inner) : base(message, inner) { } | ||
protected DetailException( | ||
System.Runtime.Serialization.SerializationInfo info, | ||
System.Runtime.Serialization.StreamingContext context) : base(info, context) { } | ||
|
||
protected DetailException( | ||
Type errorType, int errorCode, IEnumerable<ErrorParameter>? parameters, Exception? inner) : base(Enum.GetName(errorType, errorCode), inner) | ||
{ | ||
ErrorType = errorType; | ||
ErrorCode = errorCode; | ||
Parameters = parameters ?? Enumerable.Empty<ErrorParameter>(); | ||
} | ||
|
||
protected static int ValueOf<T>(T error) where T : Enum => (int)Convert.ChangeType(error, TypeCode.Int32); | ||
|
||
public Type ErrorType { get; } = typeof(NoDetailsErrorType); | ||
public int ErrorCode { get; } = (int)NoDetailsErrorType.NONE; | ||
public IEnumerable<ErrorParameter> Parameters { get; } = Enumerable.Empty<ErrorParameter>(); | ||
public bool HasDetails { get => ErrorType != typeof(NoDetailsErrorType); } | ||
|
||
public string GetErrorMessage(IErrorMessageService messageService) => | ||
_templateMatcherExpression.Replace( | ||
messageService.GetMessage(ErrorType, ErrorCode), | ||
m => Parameters.SingleOrDefault(x => x.Name == m.Groups[1].Value)?.Value ?? "null"); | ||
|
||
public IEnumerable<ErrorDetails> GetErrorDetails(IErrorMessageService messageService) => | ||
GetDetailExceptions().Select(x => | ||
new ErrorDetails( | ||
x.Message, | ||
x.ErrorType.Name, | ||
messageService.GetMessage(x.ErrorType, x.ErrorCode), | ||
x.Parameters)); | ||
|
||
private IEnumerable<DetailException> GetDetailExceptions() | ||
{ | ||
yield return this; | ||
var inner = InnerException; | ||
while (inner is not null) | ||
{ | ||
if (inner is DetailException detail && detail.ErrorType != typeof(NoDetailsErrorType)) | ||
yield return detail; | ||
inner = inner.InnerException; | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/framework/Framework.ErrorHandling.Library/ErrorDetails.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling; | ||
|
||
public record ErrorDetails( | ||
string ErrorCode, | ||
string Type, | ||
string Message, | ||
IEnumerable<ErrorParameter> Parameters | ||
); | ||
|
||
public record ErrorParameter( | ||
string Name, | ||
string Value | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/framework/Framework.ErrorHandling.Library/Library/ErrorMessageService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
using System.Collections.Immutable; | ||
|
||
namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library; | ||
|
||
public sealed class ErrorMessageService : IErrorMessageService | ||
{ | ||
private readonly IReadOnlyDictionary<Type, IReadOnlyDictionary<int, string>> _messageContainers; | ||
|
||
public ErrorMessageService(IEnumerable<IErrorMessageContainer> errorMessageContainers) | ||
{ | ||
_messageContainers = errorMessageContainers.ToImmutableDictionary(x => x.Type, x => x.MessageContainer); | ||
} | ||
|
||
public string GetMessage(Type type, int code) | ||
{ | ||
if (!_messageContainers.TryGetValue(type, out var container)) | ||
throw new ArgumentException($"unexpected type {type.Name}"); | ||
|
||
if (!container.TryGetValue(code, out var message)) | ||
throw new ArgumentException($"no message defined for {type.Name}.{Enum.GetName(type, code)}"); | ||
|
||
return message; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/framework/Framework.ErrorHandling.Library/Library/ErrorResponse.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
namespace Org.Eclipse.TractusX.Portal.Backend.Framework.ErrorHandling.Library; | ||
|
||
public record ErrorResponse( | ||
string Type, | ||
string Title, | ||
int Status, | ||
IDictionary<string, IEnumerable<string>> Errors, | ||
string ErrorId, | ||
IEnumerable<ErrorDetails>? Details | ||
); |
Oops, something went wrong.