Skip to content

Commit a6ed432

Browse files
thcedclaude
andcommitted
feat(internal): ExceptionFilter delegates to consumer ExceptionHandler
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 97c339d commit a6ed432

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.retailsvc.http.internal;
2+
3+
import com.retailsvc.http.ExceptionHandler;
4+
import com.sun.net.httpserver.Filter;
5+
import com.sun.net.httpserver.HttpExchange;
6+
import java.io.IOException;
7+
8+
public final class ExceptionFilter extends Filter {
9+
private final ExceptionHandler handler;
10+
11+
public ExceptionFilter(ExceptionHandler handler) {
12+
this.handler = handler;
13+
}
14+
15+
@Override
16+
public void doFilter(HttpExchange exchange, Chain chain) throws IOException {
17+
try {
18+
chain.doFilter(exchange);
19+
} catch (RuntimeException | IOException t) {
20+
handler.handle(exchange, t);
21+
}
22+
}
23+
24+
@Override
25+
public String description() {
26+
return "Exception filter";
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.retailsvc.http.internal;
2+
3+
import com.retailsvc.http.ExceptionHandler;
4+
import com.retailsvc.http.NotFoundException;
5+
import com.sun.net.httpserver.Filter;
6+
import com.sun.net.httpserver.HttpExchange;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.Mockito;
9+
10+
class ExceptionFilterTest {
11+
@Test
12+
void delegatesToExceptionHandler() throws Exception {
13+
HttpExchange ex = Mockito.mock(HttpExchange.class);
14+
ExceptionHandler handler = Mockito.mock(ExceptionHandler.class);
15+
Filter f = new ExceptionFilter(handler);
16+
Filter.Chain chain = Mockito.mock(Filter.Chain.class);
17+
Mockito.doThrow(new NotFoundException("x")).when(chain).doFilter(ex);
18+
f.doFilter(ex, chain);
19+
Mockito.verify(handler).handle(Mockito.eq(ex), Mockito.any(NotFoundException.class));
20+
}
21+
22+
@Test
23+
void passThroughOnSuccess() throws Exception {
24+
HttpExchange ex = Mockito.mock(HttpExchange.class);
25+
ExceptionHandler handler = Mockito.mock(ExceptionHandler.class);
26+
Filter f = new ExceptionFilter(handler);
27+
Filter.Chain chain = Mockito.mock(Filter.Chain.class);
28+
f.doFilter(ex, chain);
29+
Mockito.verify(chain).doFilter(ex);
30+
Mockito.verifyNoInteractions(handler);
31+
}
32+
}

0 commit comments

Comments
 (0)