mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	chore: return 404 when adding environment to project that doesnt exist (#4635)
## About the changes Returns a 404 response instead of 500 when trying to set an environment on a project that doesn't exist
This commit is contained in:
		
							parent
							
								
									2aa63fbacd
								
							
						
					
					
						commit
						10a62642d7
					
				@ -17,7 +17,7 @@ import {
 | 
				
			|||||||
    getStandardResponses,
 | 
					    getStandardResponses,
 | 
				
			||||||
    ProjectEnvironmentSchema,
 | 
					    ProjectEnvironmentSchema,
 | 
				
			||||||
} from '../../../openapi';
 | 
					} from '../../../openapi';
 | 
				
			||||||
import { OpenApiService } from '../../../services';
 | 
					import { OpenApiService, ProjectService } from '../../../services';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const PREFIX = '/:projectId/environments';
 | 
					const PREFIX = '/:projectId/environments';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -33,18 +33,25 @@ export default class EnvironmentsController extends Controller {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private openApiService: OpenApiService;
 | 
					    private openApiService: OpenApiService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private projectService: ProjectService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    constructor(
 | 
					    constructor(
 | 
				
			||||||
        config: IUnleashConfig,
 | 
					        config: IUnleashConfig,
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            environmentService,
 | 
					            environmentService,
 | 
				
			||||||
            openApiService,
 | 
					            openApiService,
 | 
				
			||||||
        }: Pick<IUnleashServices, 'environmentService' | 'openApiService'>,
 | 
					            projectService,
 | 
				
			||||||
 | 
					        }: Pick<
 | 
				
			||||||
 | 
					            IUnleashServices,
 | 
				
			||||||
 | 
					            'environmentService' | 'openApiService' | 'projectService'
 | 
				
			||||||
 | 
					        >,
 | 
				
			||||||
    ) {
 | 
					    ) {
 | 
				
			||||||
        super(config);
 | 
					        super(config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.logger = config.getLogger('admin-api/project/environments.ts');
 | 
					        this.logger = config.getLogger('admin-api/project/environments.ts');
 | 
				
			||||||
        this.environmentService = environmentService;
 | 
					        this.environmentService = environmentService;
 | 
				
			||||||
        this.openApiService = openApiService;
 | 
					        this.openApiService = openApiService;
 | 
				
			||||||
 | 
					        this.projectService = projectService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        this.route({
 | 
					        this.route({
 | 
				
			||||||
            method: 'post',
 | 
					            method: 'post',
 | 
				
			||||||
@ -126,6 +133,7 @@ export default class EnvironmentsController extends Controller {
 | 
				
			|||||||
    ): Promise<void> {
 | 
					    ): Promise<void> {
 | 
				
			||||||
        const { projectId } = req.params;
 | 
					        const { projectId } = req.params;
 | 
				
			||||||
        const { environment } = req.body;
 | 
					        const { environment } = req.body;
 | 
				
			||||||
 | 
					        await this.projectService.getProject(projectId); // Validates that the project exists
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await this.environmentService.addEnvironmentToProject(
 | 
					        await this.environmentService.addEnvironmentToProject(
 | 
				
			||||||
            environment,
 | 
					            environment,
 | 
				
			||||||
 | 
				
			|||||||
@ -159,3 +159,12 @@ test('Should throw an error if you try to set defaultStrategy other than flexibl
 | 
				
			|||||||
        })
 | 
					        })
 | 
				
			||||||
        .expect(400);
 | 
					        .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);
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user