diff --git a/.gitignore b/.gitignore index 2e2ad5c..53efc2f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ OpenAPIDocCtrl.cs tmp/ *~ .*~ +ENV.database diff --git a/DbService.cs b/DbService.cs index f1d2aa5..898604f 100644 --- a/DbService.cs +++ b/DbService.cs @@ -37,6 +37,8 @@ namespace de.hottis.genericdatabaseapiservice.Services { Console.WriteLine("ConnInfo: {0}", databaseConnInfo); Console.WriteLine("Statement: {0}", selectStatement); + throw new Exception("AetschiBaetsch"); + using (var conn = new MySqlConnection(databaseConnInfo)) { await conn.OpenAsync(); diff --git a/generate.py b/generate.py index 0c33174..5604bf3 100644 --- a/generate.py +++ b/generate.py @@ -13,6 +13,10 @@ parser.add_argument('--apiDefinitionFile', '-a', help='API definition file. Default: openapi.yaml in the current folder.', required=False, default='./openapi.yaml') +parser.add_argument('--errorDefinitionFile', '-e', + help='Error definition file. Default: serviceErrorCodes.yaml in the current folder.', + required=False, + default='./serviceErrorCodes.yaml') parser.add_argument('--template', '-t', help="""Template file, templates files must be named as the final output file with an additional .tmpl extension. Default: all template files recursively from the current folder.""", @@ -23,6 +27,9 @@ args = parser.parse_args() with open(args.apiDefinitionFile) as apiDefinitionFile: apiDefinition = yaml.load(apiDefinitionFile, Loader=SafeLoader) +with open(args.errorDefinitionFile) as errorDefinitionFile: + errorDefinition = yaml.load(errorDefinitionFile, Loader=SafeLoader) + apiDefinition["GENERATED_SQL_COMMENT"] = """ @@ -142,9 +149,10 @@ for (typeName, typeDefinition) in apiDefinition['components']['schemas'].items() apiDefinition['types'] = types print(json.dumps(apiDefinition, indent=4)) +print(json.dumps(errorDefinition, indent=4)) for f in glob.glob(args.template, recursive=True): print(f"process {f}") - tmpl = Template(file=f, searchList=[apiDefinition]) + tmpl = Template(file=f, searchList=[apiDefinition, errorDefinition]) with open(f[:-5], 'w') as outFile: outFile.write(str(tmpl)) diff --git a/generateAll.sh b/generateAll.sh index 53a0a4a..2e3fbf6 100755 --- a/generateAll.sh +++ b/generateAll.sh @@ -62,6 +62,11 @@ fi # PACKAGE_NAME will be loaded here . ENV +if [ -f ENV.database ]; then + echo "database environment loaded" + . ENV.database +fi + if [ "$STAGE1" = "1" ]; then echo "generate endpoint code from openapi.yaml" python3.10 generate.py diff --git a/openapi.yaml b/openapi.yaml index 2c3ec75..21c6b1c 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -97,6 +97,12 @@ paths: application/json: schema: $ref: "#/components/schemas/errorResultObject" + 500: + description: Internal Server Error + content: + application/json: + schema: + $ref: "#/components/schemas/errorResultObject" /pdb/v2/baseData/{articleNumber}: get: tags: [ "Regular" ] @@ -145,6 +151,9 @@ components: errorInfoURL: description: URL to some more information on the error type: string + offensiveData: + description: Input data which causes this error + type: string test1: description: A test1 item type: object diff --git a/regular.cs.tmpl b/regular.cs.tmpl index a911c85..452b0cb 100644 --- a/regular.cs.tmpl +++ b/regular.cs.tmpl @@ -117,15 +117,31 @@ null#slurp [0]#slurp #end if ); - } catch (NotDataFoundException) { +\#pragma warning disable CS0168 + #for $errDef in $Exceptions + } catch ($errDef['Exception'] ex) { var err = new ErrorResultObject(); - err.errorCode = 400; - err.serviceErrorCode = 517; - err.errorMessage("item not found"); - err.errorInfoURL("https://google.com"); - return BadRequest(err); - } - + err.ErrorCode = $errDef['ErrorCode']; + err.ServiceErrorCode = $errDef['ServiceErrorCode']; + err.ErrorMessage = #slurp + #if $errDef['ErrorMessage'] == 'INSERT_EXCEPTION_MESSAGE' +ex.ToString(); + #else +"$errDef['ErrorMessage']"; + #end if + err.ErrorInfoURL = "$errDef['ErrorInfoURL']"; + err.OffensiveData = #slurp + #if $operation['bodyInputType'] +Newtonsoft.Json.JsonConvert.SerializeObject($operation['bodyInputType']['apiName'], Newtonsoft.Json.Formatting.Indented); + #elif $operation['paramInputTypes'] +Newtonsoft.Json.JsonConvert.SerializeObject(paramInput, Newtonsoft.Json.Formatting.Indented); + #else +null; + #end if + return StatusCode($errDef['ErrorCode'], err); + #end for + } +\#pragma warning restore CS0168 } #elif $operation['method'] == 'post' diff --git a/serviceErrorCodes.yaml b/serviceErrorCodes.yaml new file mode 100644 index 0000000..6f6d2fc --- /dev/null +++ b/serviceErrorCodes.yaml @@ -0,0 +1,18 @@ +# ONLY USE THE ERRORCODES 400 and 500 HERE +Exceptions: + - Exception: NotDataFoundException + ErrorCode: 400 + ServiceErrorCode: 10001 + ErrorMessage: item not found + ErrorInfoURL: https://google.com + - Exception: TooMuchDataFoundException + ErrorCode: 400 + ServiceErrorCode: 10002 + ErrorMessage: too many items found + ErrorInfoURL: https://google.com + # Make sure "Exception: Exception" is the last entry in the list + - Exception: Exception + ErrorCode: 500 + ServiceErrorCode: 10000 + ErrorMessage: INSERT_EXCEPTION_MESSAGE + ErrorInfoURL: https://google.com