Skip to content

Commit

Permalink
chore: return 404 when adding environment to project that doesnt exist (
Browse files Browse the repository at this point in the history
#4635)

## About the changes

Returns a 404 response instead of 500 when trying to set an environment
on a project that doesn't exist
  • Loading branch information
daveleek committed Sep 8, 2023
1 parent 2aa63fb commit 10a6264
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib/routes/admin-api/project/environments.ts
Expand Up @@ -17,7 +17,7 @@ import {
getStandardResponses,
ProjectEnvironmentSchema,
} from '../../../openapi';
import { OpenApiService } from '../../../services';
import { OpenApiService, ProjectService } from '../../../services';

const PREFIX = '/:projectId/environments';

Expand All @@ -33,18 +33,25 @@ export default class EnvironmentsController extends Controller {

private openApiService: OpenApiService;

private projectService: ProjectService;

constructor(
config: IUnleashConfig,
{
environmentService,
openApiService,
}: Pick<IUnleashServices, 'environmentService' | 'openApiService'>,
projectService,
}: Pick<
IUnleashServices,
'environmentService' | 'openApiService' | 'projectService'
>,
) {
super(config);

this.logger = config.getLogger('admin-api/project/environments.ts');
this.environmentService = environmentService;
this.openApiService = openApiService;
this.projectService = projectService;

this.route({
method: 'post',
Expand Down Expand Up @@ -126,6 +133,7 @@ export default class EnvironmentsController extends Controller {
): Promise<void> {
const { projectId } = req.params;
const { environment } = req.body;
await this.projectService.getProject(projectId); // Validates that the project exists

await this.environmentService.addEnvironmentToProject(
environment,
Expand Down
9 changes: 9 additions & 0 deletions src/test/e2e/api/admin/project/environments.e2e.test.ts
Expand Up @@ -159,3 +159,12 @@ test('Should throw an error if you try to set defaultStrategy other than flexibl
})
.expect(400);
});

test('Add environment to project should return 404 when given a projectid that does not exist', async () => {
await app.request
.post(`/api/admin/projects/unknown/environments`)
.send({
environment: 'default',
})
.expect(404);
});

0 comments on commit 10a6264

Please sign in to comment.