Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provicevk/unified responses #843

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft

Conversation

ghost
Copy link

@ghost ghost commented Sep 7, 2022

Incoming:

  • Added ApiError model
  • Changed ProviderController responses for V1

Note that ApiError model should be used for 4xx, 5xx Http Codes.

All Api errors are introduced with a group and code. Therefore we use a dictionary of dictionaries in Frontend.

For future discussions:

  • How does Frontend handle bad responses? Do we need a lot of codes for simillar things like incorrect model for different DTOs?
  • Overriding default model validation ([ApiController]) behaviour using custom filter.
  • Do we need to provide auxiliary object in case if we need some data else (example: ModelState is not valid, hence we return it with a list of fields where an error is occured)?
  • Do we need to describe errors as Problem Details according to RFC?
  • We can create overloadings for methods like: BadRequest(ApiError error), NotFound(ApiError error) that takes ApiError as parameter.

For thinking:

  • We can try to create some type like Err in builder behavior:
    return Err.WithMessage(ProviderApiError.ProviderNotCreated).WithCode(HttpStatusCode.BadRequest);

@sonarqubecloud
Copy link

sonarqubecloud bot commented Sep 7, 2022

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@ghost ghost requested review from VadymLevkovskyi and a team September 8, 2022 13:48
@DmyMi DmyMi force-pushed the develop branch 7 times, most recently from 8fdf2a5 to f6a4108 Compare April 25, 2023 15:47
@@ -0,0 +1,23 @@
using Ardalis.SmartEnum;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Excessive code) Is this using necessary?

@@ -0,0 +1,22 @@
using Ardalis.SmartEnum;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Excessive code) Is this using necessary?

Message = message;
}

public static ApiErrorResponse Create(string groupName, string code, string message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain why instead of public ctor you have private ctor and public static? Statics are worser from testability point of view. As it does the same as ctor - what is the main reason to have it?

public class InputApiError : ApiError
{
public static readonly InputApiError InputDataIncorrect
= new (nameof(InputDataIncorrect), "Provided data input is incorrect");
Copy link
Contributor

@VadymLevkovskyi VadymLevkovskyi Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Non-uniqueness) nameof is a cool idea, but it will give you only method/prop name. Thus, in other class (i.e. child of this class named InputAdminApiError) method/prop with same name (InputDataIncorrect) could exist and you'll get eror-code collision if you forget to redefine GroupName. Please use simple enum containing all error codes, or more complex schema ot generate them - at least
$"{nameof(class)}.{nameof(method or property)}"
Or even more complex approach could be used with CallerMemberNameAttribute and it's friends, if you like it


public sealed class ProviderApiError : ApiError
{
public static readonly ProviderApiError ProviderNotCreated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment re nameof and error codes

{
}

public override string GroupName => nameof(InputApiError);
Copy link
Contributor

@VadymLevkovskyi VadymLevkovskyi Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Performance) Do you need this override? Why not set GroupName in ctor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Related to all overiden GroupNames)


public static class ApiErrorExtensions
{
public static ApiErrorResponse CreateApiErrorResponse(this ApiError apiError)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Code style) Consider necessity of this extension class.
Ususally extensions are good for code decoupling. Mapping extensions - when you have 2 classses not related to each other and you don't want introduce any hard dependency between them.

It is not the case for ApiError and ApiErrorResponse, as latter is direct child of former. So why not put this method in ApiErrorResponse class?

Copy link
Contributor

@VadymLevkovskyi VadymLevkovskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I don't get what problem you are fixing. Please explain why we need lot of inherited error types with no changes in data.
Are there any plans for extending logic or properties in future children?

Maybe it could be simpler? Like 1 class with lot of static properties for each api error code case, or simple enum, list of tuples (if you need that GroupName as separate thing), whatever?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants