Skip to content

Commit 026b237

Browse files
committed
status codes for thrown errors
1 parent 82427c0 commit 026b237

File tree

3 files changed

+68
-28
lines changed

3 files changed

+68
-28
lines changed

src/controllers/ControllerV1.ts

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Controller,
55
Delete,
66
Get,
7+
HttpStatusCodeLiteral,
78
Post,
89
Put,
910
Query,
@@ -13,10 +14,13 @@ import {
1314
Tags,
1415
TsoaResponse,
1516
} from 'tsoa';
17+
import { TerraformError } from '../interfaces/errors';
1618
import { StateLockRequest } from '../models/interfaces/StateLockRequest';
1719
import { GithubService } from '../services/GithubService';
1820
import { StateService } from '../services/StateService';
1921

22+
type HeaderType = { [key: string]: string | string[] };
23+
2024
@Route('/v1')
2125
@Tags('v1')
2226
export class ControllerV1 extends Controller {
@@ -31,9 +35,21 @@ export class ControllerV1 extends Controller {
3135
}
3236

3337
@Get()
34-
public async getState(@Request() request: HttpRequest): Promise<any> {
35-
const identity = await this.githubService.getIdentity(request);
36-
return this.stateService.getState(identity);
38+
public async getState(
39+
@Request() request: HttpRequest,
40+
@Res() res: TsoaResponse<200 | 400 | 401 | 404, any>,
41+
): Promise<TsoaResponse<HttpStatusCodeLiteral, any, HeaderType>> {
42+
try {
43+
const identity = await this.githubService.getIdentity(request);
44+
const state = await this.stateService.getState(identity);
45+
const response = res(200, state);
46+
return response;
47+
} catch (e) {
48+
if (e instanceof TerraformError) {
49+
return e.respond(res);
50+
}
51+
throw e;
52+
}
3753
}
3854

3955
@Post()
@@ -42,38 +58,59 @@ export class ControllerV1 extends Controller {
4258
@Query('ID') id: string,
4359
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
4460
@Body() state: any,
45-
@Res() res: TsoaResponse<200, void>,
46-
): Promise<void> {
47-
const stateLockRequest = await this.stateService.getRequest(id);
48-
const identity = await this.githubService.getIdentity(request, stateLockRequest);
49-
await this.stateService.saveState(identity, id, state);
50-
const response = res(200);
51-
return response;
61+
@Res() res: TsoaResponse<200 | 400 | 401 | 404 | 409, void>,
62+
): Promise<TsoaResponse<HttpStatusCodeLiteral, void, HeaderType>> {
63+
try {
64+
const stateLockRequest = await this.stateService.getRequest(id);
65+
const identity = await this.githubService.getIdentity(request, stateLockRequest);
66+
await this.stateService.saveState(identity, id, state);
67+
const response = res(200);
68+
return response;
69+
} catch (e) {
70+
if (e instanceof TerraformError) {
71+
return e.respond(res);
72+
}
73+
throw e;
74+
}
5275
}
5376

5477
@Put('lock')
5578
public async lockState(
5679
@Request() request: HttpRequest,
5780
@Body() lockRequest: StateLockRequest,
58-
@Res() res: TsoaResponse<200, boolean>,
59-
): Promise<boolean> {
60-
const stateLockRequest = await this.stateService.saveRequest(lockRequest);
61-
const identity = await this.githubService.getIdentity(request, stateLockRequest);
62-
await this.stateService.lockState(identity, stateLockRequest);
63-
const response = res(200, true);
64-
return response;
81+
@Res() res: TsoaResponse<200 | 400 | 401 | 404 | 409, boolean>,
82+
): Promise<TsoaResponse<HttpStatusCodeLiteral, boolean, HeaderType>> {
83+
try {
84+
const stateLockRequest = await this.stateService.saveRequest(lockRequest);
85+
const identity = await this.githubService.getIdentity(request, stateLockRequest);
86+
await this.stateService.lockState(identity, stateLockRequest);
87+
const response = res(200, true);
88+
return response;
89+
} catch (e) {
90+
if (e instanceof TerraformError) {
91+
return e.respond(res);
92+
}
93+
throw e;
94+
}
6595
}
6696

6797
@Delete('lock')
6898
public async unlockState(
6999
@Request() request: HttpRequest,
70100
@Body() lockRequest: StateLockRequest,
71-
@Res() res: TsoaResponse<200, boolean>,
72-
): Promise<boolean> {
73-
const stateLockRequest = await this.stateService.getRequest(lockRequest.ID);
74-
const identity = await this.githubService.getIdentity(request, stateLockRequest);
75-
await this.stateService.unlockState(identity, stateLockRequest);
76-
const response = res(200, true);
77-
return response;
101+
@Res() res: TsoaResponse<200 | 400 | 401 | 404 | 409, boolean>,
102+
): Promise<TsoaResponse<HttpStatusCodeLiteral, boolean, HeaderType>> {
103+
try {
104+
const stateLockRequest = await this.stateService.getRequest(lockRequest.ID);
105+
const identity = await this.githubService.getIdentity(request, stateLockRequest);
106+
await this.stateService.unlockState(identity, stateLockRequest);
107+
const response = res(200, true);
108+
return response;
109+
} catch (e) {
110+
if (e instanceof TerraformError) {
111+
return e.respond(res);
112+
}
113+
throw e;
114+
}
78115
}
79116
}

src/interfaces/errors.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { Response } from 'express';
1+
import { TsoaResponse } from 'tsoa';
22

33
export class TerraformError extends Error {
44
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
55
constructor(public statusCode: number, public body?: any) {
66
super();
77
}
88

9-
respond = (res: Response<any>): Response<any> => {
9+
respond = (res: TsoaResponse<any, any>): TsoaResponse<any, any> => {
1010
if (!this.body) {
11-
return res.status(this.statusCode);
11+
return res(this.statusCode, {});
1212
}
1313

14-
return res.status(this.statusCode).json(JSON.stringify(this.body));
14+
return res(this.statusCode, this.body);
1515
};
1616
}

src/services/GithubService.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ export class GithubService {
186186
if (!identity && owner && repo) {
187187
console.log(`Fetching repository ${owner}/${repo}`);
188188
const data = await octokit.repos.get({ owner, repo });
189+
190+
// TODO: Restrict access to state for public repositories?
191+
189192
identity = {
190193
pk: IdentityModel.prefix('pk', tokenSha),
191194
sk: IdentityModel.prefix('sk'),

0 commit comments

Comments
 (0)