Skip to content

Commit

Permalink
Added headers and isSwaggerException() to SwaggerException, closes #771
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Aug 22, 2017
1 parent b135aec commit 1232ec2
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public virtual string TransformText()

#line default
#line hidden
this.Write("function throwException(message: string, status: number, response: string, result" +
"?: any): Observable<any> {\r\n");
this.Write("function throwException(message: string, status: number, response: string, header" +
"s: { [key: string]: any; }, result?: any): Observable<any> {\r\n");

#line 4 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
if(Model.WrapDtoExceptions){

#line default
#line hidden
this.Write(" return Observable.throw(new SwaggerException(message, status, response, resul" +
"t));\r\n");
this.Write(" return Observable.throw(new SwaggerException(message, status, response, heade" +
"rs, result));\r\n");

#line 6 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
}else{
Expand All @@ -49,7 +49,7 @@ public virtual string TransformText()
#line hidden
this.Write(" if(result !== null && result !== undefined)\r\n return Observable.throw(" +
"result);\r\n else\r\n return Observable.throw(new SwaggerException(message" +
", status, response, null));\r\n");
", status, response, headers, null));\r\n");

#line 11 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
}
Expand All @@ -64,14 +64,16 @@ public virtual string TransformText()
#line default
#line hidden
this.Write("function throwException(q: ng.IQService, message: string, status: number, respons" +
"e: string, result?: any): ng.IPromise<any> {\r\n");
"e: string, headers: { [key: string]: any; }, result?: any): ng.IPromise<any> {\r\n" +
"");

#line 16 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
if(Model.WrapDtoExceptions){

#line default
#line hidden
this.Write(" return q.reject(new SwaggerException(message, status, response, result));\r\n");
this.Write(" return q.reject(new SwaggerException(message, status, response, headers, resu" +
"lt));\r\n");

#line 18 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
}else{
Expand All @@ -80,7 +82,7 @@ public virtual string TransformText()
#line hidden
this.Write(" if(result !== null && result !== undefined)\r\n return q.reject(result);" +
"\r\n else\r\n return q.reject(new SwaggerException(message, status, respon" +
"se, null));\r\n");
"se, headers, null));\r\n");

#line 23 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
}
Expand All @@ -94,23 +96,24 @@ public virtual string TransformText()

#line default
#line hidden
this.Write("function throwException(message: string, status: number, response: string, result" +
"?: any): any {\r\n");
this.Write("function throwException(message: string, status: number, response: string, header" +
"s: { [key: string]: any; }, result?: any): any {\r\n");

#line 28 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
if(Model.WrapDtoExceptions){

#line default
#line hidden
this.Write(" throw new SwaggerException(message, status, response, result);\r\n");
this.Write(" throw new SwaggerException(message, status, response, headers, result);\r\n");

#line 30 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
}else{

#line default
#line hidden
this.Write(" if(result !== null && result !== undefined)\r\n throw result;\r\n else\r" +
"\n throw new SwaggerException(message, status, response, null);\r\n");
"\n throw new SwaggerException(message, status, response, headers, null);\r\n" +
"");

#line 35 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\ClientMethodsTemplate.tt"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<#@ template visibility="internal" #>
<#if(Model.IsAngular){#>
function throwException(message: string, status: number, response: string, result?: any): Observable<any> {
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): Observable<any> {
<#if(Model.WrapDtoExceptions){#>
return Observable.throw(new SwaggerException(message, status, response, result));
return Observable.throw(new SwaggerException(message, status, response, headers, result));
<#}else{#>
if(result !== null && result !== undefined)
return Observable.throw(result);
else
return Observable.throw(new SwaggerException(message, status, response, null));
return Observable.throw(new SwaggerException(message, status, response, headers, null));
<#}#>
}

<#}else if(Model.IsAngularJS){#>
function throwException(q: ng.IQService, message: string, status: number, response: string, result?: any): ng.IPromise<any> {
function throwException(q: ng.IQService, message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): ng.IPromise<any> {
<#if(Model.WrapDtoExceptions){#>
return q.reject(new SwaggerException(message, status, response, result));
return q.reject(new SwaggerException(message, status, response, headers, result));
<#}else{#>
if(result !== null && result !== undefined)
return q.reject(result);
else
return q.reject(new SwaggerException(message, status, response, null));
return q.reject(new SwaggerException(message, status, response, headers, null));
<#}#>
}

<#}else{#>
function throwException(message: string, status: number, response: string, result?: any): any {
function throwException(message: string, status: number, response: string, headers: { [key: string]: any; }, result?: any): any {
<#if(Model.WrapDtoExceptions){#>
throw new SwaggerException(message, status, response, result);
throw new SwaggerException(message, status, response, headers, result);
<#}else{#>
if(result !== null && result !== undefined)
throw result;
else
throw new SwaggerException(message, status, response, null);
throw new SwaggerException(message, status, response, headers, null);
<#}#>
}

Expand Down
36 changes: 21 additions & 15 deletions src/NSwag.CodeGeneration.TypeScript/Templates/FileTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,91 +283,97 @@ public virtual string TransformText()

#line default
#line hidden
this.Write(@"export class SwaggerException extends Error {
this.Write(@"export class SwaggerException {
message: string;
status: number;
response: string;
headers: { [key: string]: any; };
result: any;
constructor(message: string, status: number, response: string, result: any) {
super();
constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
this.message = message;
this.status = status;
this.response = response;
this.headers = headers;
this.result = result;
}
private isSwaggerException = true;
static isSwaggerException(obj: any): obj is SwaggerException {
return obj.isSwaggerException === true;
}
}
");

#line 99 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 105 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
}

#line default
#line hidden

#line 100 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 106 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
if(Model.GenerateClientClasses){

#line default
#line hidden

#line 101 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 107 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(TypeScriptTemplatePartGenerator.RenderClientMethodsCode(Model, 0)));

#line default
#line hidden
this.Write("\r\n\r\n");

#line 103 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 109 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
if(Model.HandleReferences){

#line default
#line hidden

#line 104 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 110 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(Model.ReferenceHandlingCode));

#line default
#line hidden
this.Write("\r\n");

#line 105 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 111 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
}

#line default
#line hidden

#line 106 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 112 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
}

#line default
#line hidden

#line 107 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 113 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(Model.ExtensionCodeBottom));

#line default
#line hidden
this.Write("\r\n");

#line 108 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 114 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
if(Model.HasNamespace){

#line default
#line hidden
this.Write("}\r\n");

#line 110 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 116 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
}
if(Model.HasModuleName){

#line default
#line hidden
this.Write("}\r\n");

#line 113 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
#line 119 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\FileTemplate.tt"
}

#line default
Expand Down
14 changes: 10 additions & 4 deletions src/NSwag.CodeGeneration.TypeScript/Templates/FileTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,26 @@ export interface FileResponse {

<#}#>
<#if(Model.RequiresSwaggerExceptionClass){#>
export class SwaggerException extends Error {
export class SwaggerException {
message: string;
status: number;
response: string;
headers: { [key: string]: any; };
result: any;

constructor(message: string, status: number, response: string, result: any) {
super();

constructor(message: string, status: number, response: string, headers: { [key: string]: any; }, result: any) {
this.message = message;
this.status = status;
this.response = response;
this.headers = headers;
this.result = result;
}

private isSwaggerException = true;

static isSwaggerException(obj: any): obj is SwaggerException {
return obj.isSwaggerException === true;
}
}

<#}#>
Expand Down
Loading

0 comments on commit 1232ec2

Please sign in to comment.