Skip to content

Commit f206ce2

Browse files
committed
Add http error handling
1 parent 4fba494 commit f206ce2

File tree

11 files changed

+136
-8
lines changed

11 files changed

+136
-8
lines changed

apps/main/tv/codely/apps/backoffice/backend/controller/courses/CoursesGetController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public List<HashMap<String, String>> index(
4646
}
4747

4848
@Override
49-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
49+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
5050
return null;
5151
}
5252

apps/main/tv/codely/apps/backoffice/backend/controller/health_check/HealthCheckGetController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public HashMap<String, String> index() {
2929
}
3030

3131
@Override
32-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
32+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
3333
return null;
3434
}
3535
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package tv.codely.apps.mooc.backend.config;
2+
3+
import org.springframework.boot.web.servlet.FilterRegistrationBean;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
7+
import tv.codely.shared.infrastructure.spring.ApiExceptionMiddleware;
8+
9+
@Configuration
10+
public class MoocBackendServerConfiguration {
11+
private final RequestMappingHandlerMapping mapping;
12+
13+
public MoocBackendServerConfiguration(RequestMappingHandlerMapping mapping) {
14+
this.mapping = mapping;
15+
}
16+
17+
@Bean
18+
public FilterRegistrationBean<ApiExceptionMiddleware> basicHttpAuthMiddleware() {
19+
FilterRegistrationBean<ApiExceptionMiddleware> registrationBean = new FilterRegistrationBean<>();
20+
21+
registrationBean.setFilter(new ApiExceptionMiddleware(mapping));
22+
23+
return registrationBean;
24+
}
25+
}

apps/main/tv/codely/apps/mooc/backend/controller/courses/CourseGetController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ResponseEntity<HashMap<String, Serializable>> index(@PathVariable String
3838
}
3939

4040
@Override
41-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
41+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
4242
return new HashMap<Class<? extends DomainError>, HttpStatus>() {{
4343
put(CourseNotExist.class, HttpStatus.NOT_FOUND);
4444
}};

apps/main/tv/codely/apps/mooc/backend/controller/courses/CoursesPutController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ResponseEntity<String> index(
3535
}
3636

3737
@Override
38-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
38+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
3939
return null;
4040
}
4141
}

apps/main/tv/codely/apps/mooc/backend/controller/courses_counter/CoursesCounterGetController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public HashMap<String, Integer> index() throws QueryHandlerExecutionError {
2929
}
3030

3131
@Override
32-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
32+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
3333
return null;
3434
}
3535
}

apps/main/tv/codely/apps/mooc/backend/controller/health_check/HealthCheckGetController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public HashMap<String, String> index() {
2929
}
3030

3131
@Override
32-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
32+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
3333
return null;
3434
}
3535
}

apps/main/tv/codely/apps/mooc/backend/controller/notifications/NewsletterNotificationPutController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public ResponseEntity<String> index(
3434
}
3535

3636
@Override
37-
protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
37+
public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping() {
3838
return null;
3939
}
4040
}

apps/test/tv/codely/apps/mooc/backend/controller/courses/CourseGetControllerShould.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ void find_an_existing_course() throws Exception {
1414
assertResponse(String.format("/courses/%s", id), 200, body);
1515
}
1616

17+
@Test
18+
void no_find_a_non_existing_course() throws Exception {
19+
String id = "4ecc0cb3-05b2-4238-b7e1-1fbb0d5d3661";
20+
String body = "{\"error_code\": \"course_not_exist\", \"message\": \"The course <4ecc0cb3-05b2-4238-b7e1-1fbb0d5d3661> doesn't exist\"}";
21+
22+
assertResponse(String.format("/courses/%s", id), 404, body);
23+
}
24+
1725
private void givenThereIsACourse(String id, String body) throws Exception {
1826
assertRequestWithBody("PUT", String.format("/courses/%s", id), body, 201);
1927
}

src/shared/main/tv/codely/shared/infrastructure/spring/ApiController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ protected <R> R ask(Query query) throws QueryHandlerExecutionError {
2828
return queryBus.ask(query);
2929
}
3030

31-
abstract protected HashMap<Class<? extends DomainError>, HttpStatus> errorMapping();
31+
abstract public HashMap<Class<? extends DomainError>, HttpStatus> errorMapping();
3232
}

0 commit comments

Comments
 (0)