Skip to content

Commit 7522dac

Browse files
committed
Added default constructors and base classes for errors
1 parent 3ed8ab8 commit 7522dac

19 files changed

+215
-90
lines changed

src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import dev.latvian.apps.tinyserver.error.InvalidPathException;
77
import dev.latvian.apps.tinyserver.http.response.HTTPPayload;
88
import dev.latvian.apps.tinyserver.http.response.HTTPResponse;
9+
import dev.latvian.apps.tinyserver.http.response.error.client.LengthRequiredError;
910
import org.jetbrains.annotations.ApiStatus;
1011
import org.jetbrains.annotations.Nullable;
1112

@@ -147,7 +148,7 @@ public ByteBuffer bodyBuffer() throws IOException {
147148
var h = header("Content-Length");
148149

149150
if (h.isEmpty()) {
150-
throw new IOException("Content-Length header not found, chunked encoding not supported");
151+
throw new LengthRequiredError();
151152
}
152153

153154
int len = Integer.parseInt(h);

src/main/java/dev/latvian/apps/tinyserver/http/response/HTTPPayload.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void process(HTTPRequest req, int keepAliveTimeout, int maxKeepAliveConne
208208
}
209209
}
210210

211-
public void write(HTTPConnection connection, boolean writeBody) throws IOException {
211+
public void write(HTTPConnection<?> connection, boolean writeBody) throws IOException {
212212
connection.write(status.responseBuffer().duplicate());
213213

214214
int size = 2;

src/main/java/dev/latvian/apps/tinyserver/http/response/error/BadRequestError.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/dev/latvian/apps/tinyserver/http/response/error/ForbiddenError.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/dev/latvian/apps/tinyserver/http/response/error/HTTPError.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22

33
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
44

5-
public class HTTPError extends RuntimeException {
6-
private final HTTPStatus status;
5+
public abstract class HTTPError extends RuntimeException {
76
private Object extraData;
87

9-
public HTTPError(HTTPStatus status, String message) {
8+
public HTTPError() {
9+
}
10+
11+
public HTTPError(String message) {
1012
super(message);
11-
this.status = status;
1213
}
1314

14-
public HTTPError(HTTPStatus status, String message, Throwable cause) {
15+
public HTTPError(String message, Throwable cause) {
1516
super(message, cause);
16-
this.status = status;
1717
}
1818

19-
public HTTPStatus getStatus() {
20-
return status;
19+
public HTTPError(Throwable cause) {
20+
super(cause);
2121
}
2222

23+
public abstract HTTPStatus getStatus();
24+
2325
public Object getExtraData() {
2426
return extraData;
2527
}

src/main/java/dev/latvian/apps/tinyserver/http/response/error/InternalError.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/dev/latvian/apps/tinyserver/http/response/error/NotFoundError.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/dev/latvian/apps/tinyserver/http/response/error/NotImplementedError.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/dev/latvian/apps/tinyserver/http/response/error/UnauthorizedError.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.latvian.apps.tinyserver.http.response.error.client;
2+
3+
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
4+
5+
public class BadRequestError extends ClientError {
6+
public BadRequestError() {
7+
}
8+
9+
public BadRequestError(String message) {
10+
super(message);
11+
}
12+
13+
public BadRequestError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public BadRequestError(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
@Override
22+
public HTTPStatus getStatus() {
23+
return HTTPStatus.BAD_REQUEST;
24+
}
25+
}

0 commit comments

Comments
 (0)