appwrite / appwrite/sdk-generator

🚀 Feature: Constants for Error Types

Open
#698 16 comments 1 reaction 2 assignees Claimed by @stnguyen90 View on GitHub
hacktoberfest
Dominant language
Twig
Stars
325
Forks
212
Avg merge
7h 36m
Merged PRs (30d)
91

Description

### 🔖 Feature description

For the error types exposed [here](https://appwrite.io/docs/response-codes#errorTypes) we should have a class with constants or an enum so we don't have to manually put the string in the code.

### 🎤 Pitch

Let say I want to display some error message specifically to the user depending on the type, so instead of doing this:

```dart
AppWriteException exception;
if (exception.type == 'user_already_exists') {
...
}
```

I could use the constant instead
```dart
AppWriteException exception;

if (exception.type == ErrorType.userAlreadyExists) {
...
}
```

### Requirements

To implement this, we'll need to update the API specs to include the error types. For the API specs, we'll want to add an `AppwriteException` schema/definition like so:

```json
"definitions": {
"appwriteException": {
"properties": {
"message": {
"type": "string",
"description": "Error message.",
"x-example": "Invalid id: Parameter must be a valid number"
},
"type": {
"type": "string",
"description": "Error type.",
"enum": [
"general_mock",
"general_argument_invalid"
],
"x-example": "argument_invalid"
},
"code": {
"type": "integer",
"description": "Error code.",
"x-example": 400,
"format": "int32"
}
},
"x-appwrite": {
"types": [
{
"code": 400,
"type": "general_mock",
"description": "General errors thrown by the mock controller used for testing."
},
{
"code": 400,
"type": "general_argument_invalid",
"description": "The request contains one or more invalid arguments. Please refer to the endpoint documentation."
}
]
}
},
"any": {
"description": "Any",
"type": "object",
"additionalProperties": true
},
```

Note:

1. this is an example for Swagger 2. The equivalent will need to be done for OpenAPI 3
2. we're still finalizing whether was want the types in `definitions.appwriteException` or `definitions.appwriteException.properties.types`

The SDK Generator should use the API specs to generate the enums with descriptions like:

```javascript
enum ErrorType {
/**
* General errors thrown by the mock controller used for testing.
*/
GeneralMock = "general_mock",

/**
* The request contains one or more invalid arguments. Please refer to the endpoint documentation.
*/
GeneralArgumentInvalid = "general_argument_invalid"
}
```

```dart
enum ErrorType implements Comparable {
/// General errors thrown by the mock controller used for testing.
generalMock(code: 400, type: 'general_mock'),
/// General errors thrown by the mock controller used for testing.
generalArgumentInvalid(code: 400, type: 'general_argument_invalid');

const ErrorType({
required this.code,
required this.type,
});

final int code;
final String type;

@override
int compareTo(ErrorType other) => type.compareTo(other.type);
}
```

So that the developer's IDE will show the description like:

image

### Tasks

- [ ] https://github.com/appwrite/appwrite/pull/5979
- [ ] android
- [x] cli - not needed
- [ ] dart
- [ ] deno
- [ ] dotnet
- [ ] https://github.com/appwrite/sdk-for-flutter/issues/149
- [x] go - not ready
- [x] graphql/docs - not needed
- [ ] kotlin
- [ ] node
- [ ] php
- [ ] python
- [x] rest/docs - not needed
- [ ] ruby
- [ ] swift
- [ ] web

### 👀 Have you spent some time to check if this issue has been raised before?

- [X] I checked and didn't find similar issue

### 🏢 Have you read the Code of Conduct?

- [X] I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.