Skip to content

Commit 812a6e6

Browse files
committed
feat: added constants for Error Types
- added logic in Swagger2.php to parse errors from APISpecs -> definitions->x-appwrite. - added Error enums in twig templates. Signed-off-by: Jay <[email protected]>
1 parent 2069238 commit 812a6e6

File tree

10 files changed

+99
-5
lines changed

10 files changed

+99
-5
lines changed

src/Spec/Swagger2.php

+13
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ public function getDefinitions()
312312
"name" => $key,
313313
"properties" => $schema['properties'] ?? [],
314314
"description" => $schema['description'] ?? [],
315+
"error_types" => $schema['x-appwrite']['types'] ?? [],
315316
"required" => $schema['required'] ?? [],
316317
"additionalProperties" => $schema['additionalProperties'] ?? []
317318
];
@@ -337,6 +338,18 @@ public function getDefinitions()
337338
}
338339
}
339340
}
341+
if (isset($sch['error_types'])) {
342+
$types = [];
343+
foreach ($sch['error_types'] as $type) {
344+
345+
$types[] = [
346+
'code' => $type['code'],
347+
'type' => $type['type'],
348+
'description' => $type['description']
349+
];
350+
}
351+
$sch['error_types'] = $types;
352+
}
340353
$list[$key] = $sch;
341354
}
342355
return $list;

templates/android/library/src/main/java/io/appwrite/exceptions/Exception.kt.twig

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ package {{ sdk.namespace | caseDot }}.exceptions
22

33
import java.lang.Exception
44

5-
class {{spec.title | caseUcfirst}}Exception(
5+
class {{spec.title | caseUcfirst}} Exception(
66
override val message: String? = null,
77
val code: Int? = null,
88
val type: String? = null,
99
val response: String? = null
10-
) : Exception(message)
10+
) : Exception(message)
11+
12+
enum class ErrorType(val value: String) {
13+
{% for error in spec.definitions.appwriteException.error_types %}
14+
/**
15+
* {{ error.description }}
16+
*/
17+
{{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"),
18+
{% endfor %}
19+
}

templates/dart/lib/src/exception.dart.twig

+7
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ class {{spec.title | caseUcfirst}}Exception implements Exception {
2121
return "{{spec.title | caseUcfirst}}Exception: ${type ?? ''}, $message (${code ?? 0})";
2222
}
2323
}
24+
25+
enum ErrorType {
26+
{% for error in spec.definitions.appwriteException.error_types %}
27+
/// {{ error.description }}
28+
{{ error.type|title|replace({'_': ''}) }},
29+
{% endfor %}
30+
}

templates/deno/src/exception.ts.twig

+9
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ export class {{ spec.title | caseUcfirst}}Exception {
1414
public toString(): String {
1515
return `${this.message} - ${this.code} - ${this.type} - ${JSON.stringify(this.response)}`;
1616
}
17+
}
18+
19+
enum ErrorType {
20+
{% for error in spec.definitions.appwriteException.error_types %}
21+
/**
22+
* {{ error.description }}
23+
*/
24+
{{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}",
25+
{% endfor %}
1726
}

templates/dotnet/src/Appwrite/Exception.cs.twig

+8
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ namespace {{spec.title | caseUcfirst}}
2525
}
2626
}
2727

28+
public enum ErrorType {
29+
{% for error in spec.definitions.appwriteException.error_types %}
30+
/// <summary>
31+
/// {{ error.description }}
32+
/// </summary>
33+
{{ error.type|title|replace({'_': ''}) }},
34+
{% endfor %}
35+
}

templates/kotlin/src/main/kotlin/io/appwrite/exceptions/Exception.kt.twig

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ class {{spec.title | caseUcfirst}}Exception(
77
val code: Int? = null,
88
val type: String? = null,
99
val response: String? = null
10-
) : Exception(message)
10+
) : Exception(message)
11+
12+
enum class ErrorType(val value: String) {
13+
{% for error in spec.definitions.appwriteException.error_types %}
14+
/**
15+
* {{ error.description }}
16+
*/
17+
{{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"),
18+
{% endfor %}
19+
}

templates/node/lib/exception.js.twig

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,15 @@ class {{spec.title | caseUcfirst}}Exception extends Error {
77
}
88
}
99

10-
module.exports = {{spec.title | caseUcfirst}}Exception;
10+
module.exports = {{spec.title | caseUcfirst}}Exception;
11+
12+
enum ErrorType {
13+
{% for error in spec.definitions.appwriteException.error_types %}
14+
/**
15+
* {{ error.description }}
16+
*/
17+
{{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}",
18+
{% endfor %}
19+
}
20+
21+
module.exports = ErrorType;

templates/php/src/Exception.php.twig

+9
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@ class {{spec.title | caseUcfirst}}Exception extends Exception {
4343
{
4444
return $this->response;
4545
}
46+
}
47+
48+
class ErrorType {
49+
{% for error in spec.definitions.appwriteException.error_types %}
50+
/**
51+
* {{ error.description }}
52+
*/
53+
const {{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}';
54+
{% endfor %}
4655
}
+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
from enum import Enum
2+
3+
14
class {{spec.title | caseUcfirst}}Exception(Exception):
25
def __init__(self, message, code = 0, type = None, response = None):
36
self.message = message
47
self.code = code
58
self.type = type
69
self.response = response
7-
super().__init__(self.message)
10+
super().__init__(self.message)
11+
12+
13+
class ErrorType(Enum):
14+
{% for error in spec.definitions.appwriteException.error_types %}
15+
"""
16+
{{ error.description }}
17+
"""
18+
{{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}'
19+
{% endfor %}

templates/ruby/lib/container/exception.rb.twig

+7
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ module {{spec.title | caseUcfirst}}
1212
end
1313
end
1414
end
15+
16+
class ErrorType
17+
{% for error in spec.definitions.appwriteException.error_types %}
18+
# {{ error.description }}
19+
{{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}'
20+
{% endfor %}
21+
end

0 commit comments

Comments
 (0)