openflagr / flagr-archived

[Archived] - Merged and move to https://github.com/openflagr/flagr
https://openflagr.github.io/flagr
Apache License 2.0
33 stars 7 forks source link

Flag Key Unique Constraint validation should not return 500 #41

Closed marceloboeira closed 2 years ago

marceloboeira commented 2 years ago

The uniqueness constraint of flag.key is a known part of flag. Validation errors that rely on client-logic (uniqueness of a field) is responsibility of the client to implement, thus the server should return a 400-ish (e.g.: 400 Bad Request, 406 Not Acceptable, ...) and not a 500 Internal Server Error, since that is not the case.

Using httpie to exemplify:

http POST :18000/api/v1/flags Description=Sweden key=SE

HTTP/1.1 200 OK
Content-Type: application/json

{
    "dataRecordsEnabled": false,
    "description": "Sweden",
    "enabled": false,
    "id": 4,
    "key": "SE",
    "segments": [],
    "tags": [],
    "updatedAt": "2021-10-02T16:41:28.191Z",
    "variants": []
}

If you try that again, you should get something like...

Expected Behavior

http POST :18000/api/v1/flags Description=Sweden key=SE
HTTP/1.1 400 Bad Request
Content-Type: application/json

{
    "message": "cannot create flag. a flag with key=SE already exists"
}

But you end up getting a 500:

Current Behavior

http POST :18000/api/v1/flags Description=Sweden key=SE
HTTP/1.1 500 Internal Server Error
Content-Type: application/json

{
    "message": "cannot create flag. UNIQUE constraint failed: flags.key"
}

Possible Solution

Intercept this uniqueness constraint error and fail gracefully

Steps to Reproduce (for bugs)

See above...

Context

Semantically, validations errors should be client errors, 400-ish, since the server doesn't fail, yet the client fails on trying to bypass the uniqueness of the field.

Your Environment

github-actions[bot] commented 2 years ago

Stale issue message

pixeldrew commented 2 years ago

@zhouzhuojie any word on this?

github-actions[bot] commented 2 years ago

Stale issue message

bhasden commented 2 years ago

Can someone educate me on the thoughts behind having a GitHub action to automatically close issues and pull-requests? Looking back through the closed items, there appears to be a lot of work done by volunteers which was seemingly ignored. I completely understand that people get busy and projects get stale, but having the issues and pull-requests stay open would give people who are: 1) evaluating the project and 2) considering forking the project, a better idea of the project state and what can be pulled into a fork of the project.