diff --git a/src/SDK/Language/Android.php b/src/SDK/Language/Android.php index 5d3e90bb0..323fe5436 100644 --- a/src/SDK/Language/Android.php +++ b/src/SDK/Language/Android.php @@ -387,6 +387,11 @@ public function getFiles(): array 'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt', 'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig', ], + [ + 'scope' => 'default', + 'destination' => 'tests/TestException.kt', + 'template' => '/android/tests/TestException.kt.twig', + ], ]; } } diff --git a/src/SDK/Language/Deno.php b/src/SDK/Language/Deno.php index 1fdef0c0d..8650cb216 100644 --- a/src/SDK/Language/Deno.php +++ b/src/SDK/Language/Deno.php @@ -93,6 +93,11 @@ public function getFiles(): array 'destination' => 'docs/examples/{{service.name | caseLower}}/{{method.name | caseDash}}.md', 'template' => 'deno/docs/example.md.twig', ], + [ + 'scope' => 'default', + 'destination' => 'tests/test_exception.ts', + 'template' => 'deno/tests/test_exception.ts.twig', + ], ]; } diff --git a/src/SDK/Language/DotNet.php b/src/SDK/Language/DotNet.php index f569225f2..8af0c4898 100644 --- a/src/SDK/Language/DotNet.php +++ b/src/SDK/Language/DotNet.php @@ -395,7 +395,12 @@ public function getFiles(): array 'scope' => 'definition', 'destination' => '/src/{{ spec.title | caseUcfirst }}/Models/{{ definition.name | caseUcfirst | overrideIdentifier }}.cs', 'template' => 'dotnet/src/Appwrite/Models/Model.cs.twig', - ] + ], + [ + 'scope' => 'default', + 'destination' => 'tests/Test{{ spec.title | caseUcfirst }}Exception.cs', + 'template' => 'dotnet/tests/TestException.cs.twig', + ], ]; } diff --git a/src/SDK/Language/Kotlin.php b/src/SDK/Language/Kotlin.php index a7a4c5aea..be6e1cd4f 100644 --- a/src/SDK/Language/Kotlin.php +++ b/src/SDK/Language/Kotlin.php @@ -419,6 +419,11 @@ public function getFiles(): array 'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/models/{{ definition.name | caseUcfirst }}.kt', 'template' => '/kotlin/src/main/kotlin/io/appwrite/models/Model.kt.twig', ], + [ + 'scope' => 'default', + 'destination' => 'tests/TestException.kt', + 'template' => '/kotlin/tests/TestException.kt.twig', + ], ]; } diff --git a/src/SDK/Language/Node.php b/src/SDK/Language/Node.php index 435e1cce0..8dfd75e02 100644 --- a/src/SDK/Language/Node.php +++ b/src/SDK/Language/Node.php @@ -126,6 +126,11 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'node/.travis.yml.twig', ], + [ + 'scope' => 'default', + 'destination' => '/tests/test_exception.js', + 'template' => 'node/tests/test_exception.js.twig', + ], ]; } diff --git a/src/SDK/Language/PHP.php b/src/SDK/Language/PHP.php index 99ad8740e..a04c0f63b 100644 --- a/src/SDK/Language/PHP.php +++ b/src/SDK/Language/PHP.php @@ -212,6 +212,11 @@ public function getFiles(): array 'destination' => '/src/{{ spec.title | caseUcfirst}}/Services/{{service.name | caseUcfirst}}.php', 'template' => 'php/src/Services/Service.php.twig', ], + [ + 'scope' => 'default', + 'destination' => 'tests/TestException.php', + 'template' => 'php/tests/TestException.php.twig', + ], ]; } diff --git a/src/SDK/Language/Python.php b/src/SDK/Language/Python.php index c8d19e639..9ea9581d4 100644 --- a/src/SDK/Language/Python.php +++ b/src/SDK/Language/Python.php @@ -184,6 +184,11 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'python/.travis.yml.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/{{ spec.title | caseSnake}}/test_exception.py', + 'template' => 'python/tests/package/test_exception.py.twig', + ], ]; } diff --git a/src/SDK/Language/Ruby.php b/src/SDK/Language/Ruby.php index eb50551b0..4caa1c578 100644 --- a/src/SDK/Language/Ruby.php +++ b/src/SDK/Language/Ruby.php @@ -182,6 +182,11 @@ public function getFiles(): array 'destination' => '/lib/{{ spec.title | caseDash }}/models/{{ definition.name | caseSnake }}.rb', 'template' => 'ruby/lib/container/models/model.rb.twig', ], + [ + 'scope' => 'default', + 'destination' => 'test/lib/{{ spec.title | caseDash }}/test_exception.rb', + 'template' => 'ruby/tests/lib/appwrite/test_exception.rb.twig', + ], ]; } diff --git a/src/SDK/Language/Swift.php b/src/SDK/Language/Swift.php index 9b2e08798..c9893cd10 100644 --- a/src/SDK/Language/Swift.php +++ b/src/SDK/Language/Swift.php @@ -284,6 +284,16 @@ public function getFiles(): array 'destination' => '/Sources/{{ spec.title | caseUcfirst}}Models/{{ definition.name | caseUcfirst }}.swift', 'template' => '/swift/Sources/Models/Model.swift.twig', ], + [ + 'scope' => 'default', + 'destination' => '/Sources/{{ spec.title | caseUcfirst}}/Exception.swift', + 'template' => 'swift/Sources/Exception.swift.twig', + ], + [ + 'scope' => 'default', + 'destination' => '/Tests/{{ spec.title | caseUcfirst}}Tests/TestException.swift', + 'template' => 'swift/Tests/TestException.swift.twig', + ], ]; } diff --git a/src/SDK/Language/Web.php b/src/SDK/Language/Web.php index 26ec51136..e0fd13d96 100644 --- a/src/SDK/Language/Web.php +++ b/src/SDK/Language/Web.php @@ -115,6 +115,16 @@ public function getFiles(): array 'destination' => '.travis.yml', 'template' => 'web/.travis.yml.twig', ], + [ + 'scope' => 'default', + 'destination' => 'tests/test_exception.ts', + 'template' => 'web/tests/test_exception.ts.twig', + ], + [ + 'scope' => 'default', + 'destination' => 'src/exception.ts', + 'template' => 'web/src/exception.ts.twig', + ], ]; } diff --git a/src/SDK/SDK.php b/src/SDK/SDK.php index 73a75ce39..4294d9141 100644 --- a/src/SDK/SDK.php +++ b/src/SDK/SDK.php @@ -555,7 +555,7 @@ public function generate(string $target): void 'name' => $this->language->getName(), 'params' => $this->language->getParams(), ], - 'sdk' => $this->getParams(), + 'sdk' => $this->getParams() ]; foreach ($this->language->getFiles() as $file) { @@ -602,6 +602,10 @@ public function generate(string $target): void foreach ($this->spec->getDefinitions() as $key => $definition) { $params['definition'] = $definition; + if (strpos($definition['name'], "Exception")) { + continue; + } + if ($this->exclude($file, $params)) { continue; } diff --git a/src/Spec/Swagger2.php b/src/Spec/Swagger2.php index bf343ea83..30fa693b6 100644 --- a/src/Spec/Swagger2.php +++ b/src/Spec/Swagger2.php @@ -312,6 +312,7 @@ public function getDefinitions() "name" => $key, "properties" => $schema['properties'] ?? [], "description" => $schema['description'] ?? [], + "errorTypes" => $schema['x-appwrite']['types'] ?? null, "required" => $schema['required'] ?? [], "additionalProperties" => $schema['additionalProperties'] ?? [] ]; @@ -337,6 +338,17 @@ public function getDefinitions() } } } + if (isset($sch['errorTypes'])) { + $types = []; + foreach ($sch['errorTypes'] as $type) { + $types[] = [ + 'code' => $type['code'], + 'type' => $type['type'], + 'message' => $type['message'] + ]; + } + $sch['errorTypes'] = $types; + } $list[$key] = $sch; } return $list; diff --git a/templates/android/library/src/main/java/io/appwrite/exceptions/Exception.kt.twig b/templates/android/library/src/main/java/io/appwrite/exceptions/Exception.kt.twig index b081940d0..e62612cd1 100644 --- a/templates/android/library/src/main/java/io/appwrite/exceptions/Exception.kt.twig +++ b/templates/android/library/src/main/java/io/appwrite/exceptions/Exception.kt.twig @@ -7,4 +7,15 @@ class {{spec.title | caseUcfirst}}Exception( val code: Int? = null, val type: String? = null, val response: String? = null -) : Exception(message) \ No newline at end of file +) : Exception(message) + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +enum class ErrorType(val value: String) { +{% for error in spec.definitions.appwriteException.errorTypes %} + /** + * {{ error.message }} + */ + {{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"), +{% endfor %} +} +{% endif %} \ No newline at end of file diff --git a/templates/android/tests/TestException.kt.twig b/templates/android/tests/TestException.kt.twig new file mode 100644 index 000000000..d959bd55f --- /dev/null +++ b/templates/android/tests/TestException.kt.twig @@ -0,0 +1,17 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +package tests + +import {{ sdk.namespace | caseDot }}.exceptions.ErrorType +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class TestException { + + @Test + fun testEnumValues() { + {% for error in spec.definitions.appwriteException.errorTypes %} + assertEquals(ErrorType.{{ error.type|title|replace({'_': ''}) }}.value), "{{ error.type }}") + {% endfor %} +} +} +{% endif %} \ No newline at end of file diff --git a/templates/dart/lib/models.dart.twig b/templates/dart/lib/models.dart.twig index 1a15137f2..476c8c512 100644 --- a/templates/dart/lib/models.dart.twig +++ b/templates/dart/lib/models.dart.twig @@ -3,5 +3,7 @@ library {{ language.params.packageName }}.models; part 'src/models/model.dart'; {% for definition in spec.definitions %} +{% if 'Exception' not in definition.name %} part 'src/models/{{definition.name | caseSnake}}.dart'; +{% endif %} {% endfor %} \ No newline at end of file diff --git a/templates/dart/lib/src/exception.dart.twig b/templates/dart/lib/src/exception.dart.twig index dfb1816f4..7e5fb564d 100644 --- a/templates/dart/lib/src/exception.dart.twig +++ b/templates/dart/lib/src/exception.dart.twig @@ -21,3 +21,12 @@ class {{spec.title | caseUcfirst}}Exception implements Exception { return "{{spec.title | caseUcfirst}}Exception: ${type ?? ''}, $message (${code ?? 0})"; } } + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +enum ErrorType { +{% for error in spec.definitions.appwriteException.errorTypes %} + /// {{ error.message }} + {{ error.type|title|replace({'_': ''}) }}, +{% endfor %} +} +{% endif %} diff --git a/templates/dart/test/src/exception_test.dart.twig b/templates/dart/test/src/exception_test.dart.twig index efaf713d5..714e86a13 100644 --- a/templates/dart/test/src/exception_test.dart.twig +++ b/templates/dart/test/src/exception_test.dart.twig @@ -18,4 +18,12 @@ void main() { expect(exception3.toString(), equals('{{spec.title | caseUcfirst}}Exception: ValidationError, Invalid request (400)')); }); }); + + group('ErrorType Enum Test', () { + {% for key, value in testData.enumData %} + test('ErrorType.{{ key }} should have correct value', () { + expect(ErrorType.{{ error.type|title|replace({'_': ''}) }}.toString(), equals('ErrorType.{{ error.type }}')); + }); + {% endfor %} + }); } diff --git a/templates/deno/src/exception.ts.twig b/templates/deno/src/exception.ts.twig index 61b90cff5..6653a3552 100644 --- a/templates/deno/src/exception.ts.twig +++ b/templates/deno/src/exception.ts.twig @@ -14,4 +14,15 @@ export class {{ spec.title | caseUcfirst}}Exception { public toString(): String { return `${this.message} - ${this.code} - ${this.type} - ${JSON.stringify(this.response)}`; } -} \ No newline at end of file +} + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +export enum ErrorType { +{% for error in spec.definitions.appwriteException.errorTypes %} + /** + * {{ error.message }} + */ + {{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}", +{% endfor %} +} +{% endif %} \ No newline at end of file diff --git a/templates/deno/tests/test_exception.ts.twig b/templates/deno/tests/test_exception.ts.twig new file mode 100644 index 000000000..d5ee138eb --- /dev/null +++ b/templates/deno/tests/test_exception.ts.twig @@ -0,0 +1,10 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +import {assertEquals} from "https://deno.land/std/testing/asserts.ts"; +import {ErrorType} from "../src/exception.ts"; + +Deno.test("ErrorType values should match expected strings", () => { + {% for error in spec.definitions.appwriteException.errorTypes %} + assertEquals(ErrorType.{{ error.type|title|replace({'_': ''}) }}, '{{ error.type }}'); + {% endfor %} +}) +{% endif %} \ No newline at end of file diff --git a/templates/dotnet/src/Appwrite/Exception.cs.twig b/templates/dotnet/src/Appwrite/Exception.cs.twig index e78d78c2c..f23d857d4 100644 --- a/templates/dotnet/src/Appwrite/Exception.cs.twig +++ b/templates/dotnet/src/Appwrite/Exception.cs.twig @@ -23,5 +23,15 @@ namespace {{spec.title | caseUcfirst}} { } } -} +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} + public enum ErrorType { + {% for error in spec.definitions.appwriteException.errorTypes %} + /// + /// {{ error.message }} + /// + {{ error.type|title|replace({'_': ''}) }}, + {% endfor %} + } +} +{% endif %} \ No newline at end of file diff --git a/templates/dotnet/tests/TestException.cs.twig b/templates/dotnet/tests/TestException.cs.twig new file mode 100644 index 000000000..c1fb4d8e5 --- /dev/null +++ b/templates/dotnet/tests/TestException.cs.twig @@ -0,0 +1,16 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +using Microsoft.VisualStudio.TestTools.UnitTesting; +using {{spec.title | caseUcfirst}}; + +[TestClass] +public class TestException +{ + [TestMethod] + public void TestEnumValues() + { + {% for error in spec.definitions.appwriteException.errorTypes %} + Assert.AreEqual(ErrorType.{{ error.type|title|replace({'_': ''}) }}, "{{ error.type }}"); + {% endfor %} +} +} +{% endif %} diff --git a/templates/kotlin/src/main/kotlin/io/appwrite/exceptions/Exception.kt.twig b/templates/kotlin/src/main/kotlin/io/appwrite/exceptions/Exception.kt.twig index b081940d0..e62612cd1 100644 --- a/templates/kotlin/src/main/kotlin/io/appwrite/exceptions/Exception.kt.twig +++ b/templates/kotlin/src/main/kotlin/io/appwrite/exceptions/Exception.kt.twig @@ -7,4 +7,15 @@ class {{spec.title | caseUcfirst}}Exception( val code: Int? = null, val type: String? = null, val response: String? = null -) : Exception(message) \ No newline at end of file +) : Exception(message) + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +enum class ErrorType(val value: String) { +{% for error in spec.definitions.appwriteException.errorTypes %} + /** + * {{ error.message }} + */ + {{ error.type|title|replace({'_': ''}) }}("{{ error.type }}"), +{% endfor %} +} +{% endif %} \ No newline at end of file diff --git a/templates/kotlin/tests/TestException.kt.twig b/templates/kotlin/tests/TestException.kt.twig new file mode 100644 index 000000000..d959bd55f --- /dev/null +++ b/templates/kotlin/tests/TestException.kt.twig @@ -0,0 +1,17 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +package tests + +import {{ sdk.namespace | caseDot }}.exceptions.ErrorType +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class TestException { + + @Test + fun testEnumValues() { + {% for error in spec.definitions.appwriteException.errorTypes %} + assertEquals(ErrorType.{{ error.type|title|replace({'_': ''}) }}.value), "{{ error.type }}") + {% endfor %} +} +} +{% endif %} \ No newline at end of file diff --git a/templates/node/lib/client.js.twig b/templates/node/lib/client.js.twig index c3b8130e5..71e4c02d6 100644 --- a/templates/node/lib/client.js.twig +++ b/templates/node/lib/client.js.twig @@ -3,7 +3,7 @@ const URL = require('url').URL; const https = require("https"); const axios = require('axios'); const FormData = require('form-data'); -const {{spec.title | caseUcfirst}}Exception = require('./exception.js'); +const { {{spec.title | caseUcfirst}}Exception } = require('./exception.js'); class Client { static CHUNK_SIZE = 5*1024*1024; // 5MB diff --git a/templates/node/lib/exception.js.twig b/templates/node/lib/exception.js.twig index 4520954e7..33b407d51 100644 --- a/templates/node/lib/exception.js.twig +++ b/templates/node/lib/exception.js.twig @@ -7,4 +7,20 @@ class {{spec.title | caseUcfirst}}Exception extends Error { } } -module.exports = {{spec.title | caseUcfirst}}Exception; \ No newline at end of file +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +const ErrorType = { +{% for error in spec.definitions.appwriteException.errorTypes %} + /** + * {{ error.message }} + */ + {{ error.type|title|replace({'_': ''}) }}: "{{ error.type }}", +{% endfor %} +} +{% endif %} + +module.exports = { + {{spec.title | caseUcfirst}}Exception, +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} + ErrorType +{% endif %} +}; \ No newline at end of file diff --git a/templates/node/lib/services/service.js.twig b/templates/node/lib/services/service.js.twig index acfa340a3..09860aa40 100644 --- a/templates/node/lib/services/service.js.twig +++ b/templates/node/lib/services/service.js.twig @@ -1,5 +1,5 @@ const Service = require('../service.js'); -const {{spec.title | caseUcfirst}}Exception = require('../exception.js'); +const { {{spec.title | caseUcfirst}}Exception } = require('../exception.js'); const InputFile = require('../inputFile.js'); const client = require('../client.js'); const Stream = require('stream'); diff --git a/templates/node/tests/test_exception.js.twig b/templates/node/tests/test_exception.js.twig new file mode 100644 index 000000000..41b115253 --- /dev/null +++ b/templates/node/tests/test_exception.js.twig @@ -0,0 +1,14 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +const assert = require('assert'); +const { ErrorType } = require('../lib/exception.js'); + +try { +{% for error in spec.definitions.appwriteException.errorTypes %} + assert.strictEqual(ErrorType.{{ error.type|title|replace({'_': ''}) }}, '{{ error.type }}'); +{% endfor %} + + console.log('All tests passed!'); +} catch (error) { + console.error('Test failed:', error); +} +{% endif %} diff --git a/templates/php/src/Exception.php.twig b/templates/php/src/Exception.php.twig index 0507a70a6..d5f207332 100644 --- a/templates/php/src/Exception.php.twig +++ b/templates/php/src/Exception.php.twig @@ -43,4 +43,15 @@ class {{spec.title | caseUcfirst}}Exception extends Exception { { return $this->response; } -} \ No newline at end of file +} + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +class ErrorType { +{% for error in spec.definitions.appwriteException.errorTypes %} + /** + * {{ error.message }} + */ + const {{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}'; +{% endfor %} +} +{% endif %} \ No newline at end of file diff --git a/templates/php/tests/TestException.php.twig b/templates/php/tests/TestException.php.twig new file mode 100644 index 000000000..f8d76c525 --- /dev/null +++ b/templates/php/tests/TestException.php.twig @@ -0,0 +1,12 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +use PHPUnit\Framework\TestCase; + +class TestErrorEnum extends TestCase { + + public function testEnumValues() { + {% for error in spec.definitions.appwriteException.errorTypes %} + $this->assertEquals(ErrorType::{{ error.type|title|replace({'_': ''}) }}, '{{ error.type }}'); + {% endfor %} +} +} +{% endif %} \ No newline at end of file diff --git a/templates/python/package/exception.py.twig b/templates/python/package/exception.py.twig index 6e5d4c8ff..a11fd6b84 100644 --- a/templates/python/package/exception.py.twig +++ b/templates/python/package/exception.py.twig @@ -1,7 +1,20 @@ +from enum import Enum + + class {{spec.title | caseUcfirst}}Exception(Exception): - def __init__(self, message, code = 0, type = None, response = None): + 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) \ No newline at end of file + super().__init__(self.message) + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +class ErrorType(Enum): +{% for error in spec.definitions.appwriteException.errorTypes %} + """ + {{ error.message }} + """ + {{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}' +{% endfor %} +{% endif %} \ No newline at end of file diff --git a/templates/python/tests/package/test_exception.py.twig b/templates/python/tests/package/test_exception.py.twig new file mode 100644 index 000000000..a3aad0e5f --- /dev/null +++ b/templates/python/tests/package/test_exception.py.twig @@ -0,0 +1,12 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +from ...{{ spec.title | caseSnake}}.exception import (ErrorType) +import unittest + + +class TestErrorEnum(unittest.TestCase): + + def test_enum_values(self): + {% for error in spec.definitions.appwriteException.errorTypes %} + self.assertEqual(ErrorType.{{ error.type|title|replace({'_': ''}) }}.value, '{{ error.type }}') + {% endfor %} +{% endif %} diff --git a/templates/ruby/Gemfile.twig b/templates/ruby/Gemfile.twig index cd8aa9e04..2f8fb142c 100644 --- a/templates/ruby/Gemfile.twig +++ b/templates/ruby/Gemfile.twig @@ -1,3 +1,4 @@ source 'https://rubygems.org' -gemspec \ No newline at end of file +gemspec +gem 'test-unit' \ No newline at end of file diff --git a/templates/ruby/lib/container.rb.twig b/templates/ruby/lib/container.rb.twig index 688d54357..e6f947f63 100644 --- a/templates/ruby/lib/container.rb.twig +++ b/templates/ruby/lib/container.rb.twig @@ -12,8 +12,10 @@ require_relative '{{ spec.title | caseSnake }}/permission' require_relative '{{ spec.title | caseSnake }}/role' require_relative '{{ spec.title | caseSnake }}/id' -{% for defintion in spec.definitions %} -require_relative '{{ spec.title | caseSnake }}/models/{{ defintion.name | caseSnake }}' +{% for definition in spec.definitions %} +{% if 'Exception' not in definition.name %} +require_relative '{{ spec.title | caseSnake }}/models/{{ definition.name | caseSnake }}' +{% endif %} {% endfor %} {% for service in spec.services %} diff --git a/templates/ruby/lib/container/exception.rb.twig b/templates/ruby/lib/container/exception.rb.twig index 0712d8e65..ec11b8e0d 100644 --- a/templates/ruby/lib/container/exception.rb.twig +++ b/templates/ruby/lib/container/exception.rb.twig @@ -11,4 +11,14 @@ module {{spec.title | caseUcfirst}} @response = response end end + + +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +class ErrorType +{% for error in spec.definitions.appwriteException.errorTypes %} + # {{ error.message }} + {{ error.type|title|replace({'_': ''}) }} = '{{ error.type }}' +{% endfor %} end +{% endif %} +end \ No newline at end of file diff --git a/templates/ruby/tests/lib/appwrite/test_exception.rb.twig b/templates/ruby/tests/lib/appwrite/test_exception.rb.twig new file mode 100644 index 000000000..3197357db --- /dev/null +++ b/templates/ruby/tests/lib/appwrite/test_exception.rb.twig @@ -0,0 +1,16 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +require 'test/unit' +require_relative '../../../lib/{{ spec.title | caseSnake }}/exception' + +module {{spec.title | caseUcfirst}} +class ErrorTypeTest < Test::Unit::TestCase + + def test_error_type_values + {% for error in spec.definitions.appwriteException.errorTypes %} + assert_equal('{{ error.type }}', ErrorType::{{ error.type|title|replace({'_': ''}) }}) + {% endfor %} + end + end +end + +{% endif %} diff --git a/templates/swift/Sources/Exception.swift.twig b/templates/swift/Sources/Exception.swift.twig new file mode 100644 index 000000000..5b85909c0 --- /dev/null +++ b/templates/swift/Sources/Exception.swift.twig @@ -0,0 +1,8 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +enum ErrorType: String { +{% for error in spec.definitions.appwriteException.errorTypes %} + /// {{ error.message }} + case {{ error.type | caseCamel }} = "{{ error.type }}" +{% endfor %} +} +{% endif %} \ No newline at end of file diff --git a/templates/swift/Tests/TestException.swift.twig b/templates/swift/Tests/TestException.swift.twig new file mode 100644 index 000000000..4dd42fa12 --- /dev/null +++ b/templates/swift/Tests/TestException.swift.twig @@ -0,0 +1,12 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +import XCTest +@testable import {{spec.title | caseUcfirst}} + +class ErrorTypeTests: XCTestCase { + func testErrorTypeValues() { + {% for error in spec.definitions.appwriteException.errorTypes %} + XCTAssertEqual(ErrorType.{{ error.type | caseCamel }}.rawValue, "{{ error.type }}") + {% endfor %} +} +} +{% endif %} \ No newline at end of file diff --git a/templates/web/src/exception.ts.twig b/templates/web/src/exception.ts.twig new file mode 100644 index 000000000..618b0d4e6 --- /dev/null +++ b/templates/web/src/exception.ts.twig @@ -0,0 +1,10 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +export enum ErrorType { + {% for error in spec.definitions.appwriteException.errorTypes %} + /** + * {{ error.message }} + */ + {{ error.type|title|replace({'_': ''}) }} = "{{ error.type }}", +{% endfor %} +} +{% endif %} \ No newline at end of file diff --git a/templates/web/tests/test_exception.ts.twig b/templates/web/tests/test_exception.ts.twig new file mode 100644 index 000000000..ad9db34fe --- /dev/null +++ b/templates/web/tests/test_exception.ts.twig @@ -0,0 +1,14 @@ +{% if spec.definitions.appwriteException.errorTypes|length > 0 %} +import * as assert from "assert"; +import { ErrorType } from "../src/exception.ts" + +try { + {% for error in spec.definitions.appwriteException.errorTypes %} + assert.strictEqual(ErrorType.{{ error.type|title|replace({'_': ''}) }}, '{{ error.type }}'); + {% endfor %} + + console.log('All tests passed!'); +} catch (error) { + console.error('Test failed:', error); +} +{% endif %} \ No newline at end of file diff --git a/tests/resources/spec.json b/tests/resources/spec.json index 772602681..5c7eb3c78 100644 --- a/tests/resources/spec.json +++ b/tests/resources/spec.json @@ -1134,6 +1134,51 @@ } }, "required": ["result"] + }, + "appwriteException": { + "description": "Error Types", + "type": "object", + "properties": { + "description": { + "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", + "message": "General errors thrown by the mock controller used for testing." + }, + { + "code": 409, + "type": "document_already_exists", + "message": "Document with the requested ID already exists. Try again with a different ID or use unique() to generate a unique ID." + }, + { + "code": 404, + "type": "avatar_not_found", + "message": "The request avatar could not be found." + } + ] + } } }, "externalDocs": {