Skip to content

Commit

Permalink
feat: added constants for Error Types
Browse files Browse the repository at this point in the history
- added logic in Swagger2.php to parse errors from APISpecs -> definitions->x-appwrite.
- added Error enums in twig templates.

Signed-off-by: Jay <jaykumar20march@gmail.com>
  • Loading branch information
35C4n0r committed Oct 10, 2023
1 parent 2069238 commit 812a6e6
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/Spec/Swagger2.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public function getDefinitions()
"name" => $key,
"properties" => $schema['properties'] ?? [],
"description" => $schema['description'] ?? [],
"error_types" => $schema['x-appwrite']['types'] ?? [],
"required" => $schema['required'] ?? [],
"additionalProperties" => $schema['additionalProperties'] ?? []
];
Expand All @@ -337,6 +338,18 @@ public function getDefinitions()
}
}
}
if (isset($sch['error_types'])) {
$types = [];
foreach ($sch['error_types'] as $type) {

$types[] = [
'code' => $type['code'],
'type' => $type['type'],
'description' => $type['description']
];
}
$sch['error_types'] = $types;
}
$list[$key] = $sch;
}
return $list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@ package {{ sdk.namespace | caseDot }}.exceptions

import java.lang.Exception

class {{spec.title | caseUcfirst}}Exception(
class {{spec.title | caseUcfirst}} Exception(
override val message: String? = null,
val code: Int? = null,
val type: String? = null,
val response: String? = null
) : Exception(message)
) : Exception(message)

enum class ErrorType(val value: String) {
{% for error in spec.definitions.appwriteException.error_types %}
/**
* {{ error.description }}
*/
{{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"),
{% endfor %}
}
7 changes: 7 additions & 0 deletions templates/dart/lib/src/exception.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@ class {{spec.title | caseUcfirst}}Exception implements Exception {
return "{{spec.title | caseUcfirst}}Exception: ${type ?? ''}, $message (${code ?? 0})";
}
}

enum ErrorType {
{% for error in spec.definitions.appwriteException.error_types %}
/// {{ error.description }}
{{ error.type|title|replace({'_': ''}) }},
{% endfor %}
}
9 changes: 9 additions & 0 deletions templates/deno/src/exception.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ export class {{ spec.title | caseUcfirst}}Exception {
public toString(): String {
return `${this.message} - ${this.code} - ${this.type} - ${JSON.stringify(this.response)}`;
}
}

enum ErrorType {
{% for error in spec.definitions.appwriteException.error_types %}
/**
* {{ error.description }}
*/
{{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}",
{% endfor %}
}
8 changes: 8 additions & 0 deletions templates/dotnet/src/Appwrite/Exception.cs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ namespace {{spec.title | caseUcfirst}}
}
}

public enum ErrorType {
{% for error in spec.definitions.appwriteException.error_types %}
/// <summary>
/// {{ error.description }}
/// </summary>
{{ error.type|title|replace({'_': ''}) }},
{% endfor %}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ class {{spec.title | caseUcfirst}}Exception(
val code: Int? = null,
val type: String? = null,
val response: String? = null
) : Exception(message)
) : Exception(message)

enum class ErrorType(val value: String) {
{% for error in spec.definitions.appwriteException.error_types %}
/**
* {{ error.description }}
*/
{{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"),
{% endfor %}
}
13 changes: 12 additions & 1 deletion templates/node/lib/exception.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
}
}

module.exports = {{spec.title | caseUcfirst}}Exception;
module.exports = {{spec.title | caseUcfirst}}Exception;

enum ErrorType {
{% for error in spec.definitions.appwriteException.error_types %}
/**
* {{ error.description }}
*/
{{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}",
{% endfor %}
}

module.exports = ErrorType;
9 changes: 9 additions & 0 deletions templates/php/src/Exception.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
{
return $this->response;
}
}
class ErrorType {
{% for error in spec.definitions.appwriteException.error_types %}
/**
* {{ error.description }}
*/
const {{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}';
{% endfor %}
}
14 changes: 13 additions & 1 deletion templates/python/package/exception.py.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
from enum import Enum


class {{spec.title | caseUcfirst}}Exception(Exception):
def __init__(self, message, code = 0, type = None, response = None):
self.message = message
self.code = code
self.type = type
self.response = response
super().__init__(self.message)
super().__init__(self.message)


class ErrorType(Enum):
{% for error in spec.definitions.appwriteException.error_types %}
"""
{{ error.description }}
"""
{{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}'
{% endfor %}
7 changes: 7 additions & 0 deletions templates/ruby/lib/container/exception.rb.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ module {{spec.title | caseUcfirst}}
end
end
end

class ErrorType
{% for error in spec.definitions.appwriteException.error_types %}
# {{ error.description }}
{{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}'
{% endfor %}
end

0 comments on commit 812a6e6

Please sign in to comment.