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

Diff for: src/main/java/dev/latvian/apps/tinyserver/http/HTTPRequest.java

+2-1
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);

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

+1-1
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;

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

-13
This file was deleted.

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

-13
This file was deleted.

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

+10-8
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
}

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

-13
This file was deleted.

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

-13
This file was deleted.

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

-13
This file was deleted.

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

-13
This file was deleted.
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.latvian.apps.tinyserver.http.response.error.client;
2+
3+
import dev.latvian.apps.tinyserver.http.response.error.HTTPError;
4+
5+
public abstract class ClientError extends HTTPError {
6+
public ClientError() {
7+
}
8+
9+
public ClientError(String message) {
10+
super(message);
11+
}
12+
13+
public ClientError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public ClientError(Throwable cause) {
18+
super(cause);
19+
}
20+
}
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 ForbiddenError extends ClientError {
6+
public ForbiddenError() {
7+
}
8+
9+
public ForbiddenError(String message) {
10+
super(message);
11+
}
12+
13+
public ForbiddenError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public ForbiddenError(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
@Override
22+
public HTTPStatus getStatus() {
23+
return HTTPStatus.FORBIDDEN;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package dev.latvian.apps.tinyserver.http.response.error.client;
2+
3+
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
4+
5+
public class LengthRequiredError extends ClientError {
6+
@Override
7+
public HTTPStatus getStatus() {
8+
return HTTPStatus.LENGTH_REQUIRED;
9+
}
10+
}
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 NotFoundError extends ClientError {
6+
public NotFoundError() {
7+
}
8+
9+
public NotFoundError(String message) {
10+
super(message);
11+
}
12+
13+
public NotFoundError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public NotFoundError(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
@Override
22+
public HTTPStatus getStatus() {
23+
return HTTPStatus.NOT_FOUND;
24+
}
25+
}
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 UnauthorizedError extends ClientError {
6+
public UnauthorizedError() {
7+
}
8+
9+
public UnauthorizedError(String message) {
10+
super(message);
11+
}
12+
13+
public UnauthorizedError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public UnauthorizedError(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
@Override
22+
public HTTPStatus getStatus() {
23+
return HTTPStatus.UNAUTHORIZED;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.latvian.apps.tinyserver.http.response.error.server;
2+
3+
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
4+
5+
public class InternalError extends ServerError {
6+
public InternalError() {
7+
}
8+
9+
public InternalError(String message) {
10+
super(message);
11+
}
12+
13+
public InternalError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public InternalError(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
@Override
22+
public HTTPStatus getStatus() {
23+
return HTTPStatus.INTERNAL_ERROR;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package dev.latvian.apps.tinyserver.http.response.error.server;
2+
3+
import dev.latvian.apps.tinyserver.http.response.HTTPStatus;
4+
5+
public class NotImplementedError extends ServerError {
6+
public NotImplementedError() {
7+
}
8+
9+
public NotImplementedError(String message) {
10+
super(message);
11+
}
12+
13+
public NotImplementedError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public NotImplementedError(Throwable cause) {
18+
super(cause);
19+
}
20+
21+
@Override
22+
public HTTPStatus getStatus() {
23+
return HTTPStatus.NOT_IMPLEMENTED;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dev.latvian.apps.tinyserver.http.response.error.server;
2+
3+
import dev.latvian.apps.tinyserver.http.response.error.HTTPError;
4+
5+
public abstract class ServerError extends HTTPError {
6+
public ServerError() {
7+
}
8+
9+
public ServerError(String message) {
10+
super(message);
11+
}
12+
13+
public ServerError(String message, Throwable cause) {
14+
super(message, cause);
15+
}
16+
17+
public ServerError(Throwable cause) {
18+
super(cause);
19+
}
20+
}

Diff for: src/test/java/dev/latvian/apps/tinyserver/test/TinyServerTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import dev.latvian.apps.tinyserver.HTTPConnection;
44
import dev.latvian.apps.tinyserver.http.response.HTTPResponse;
5-
import dev.latvian.apps.tinyserver.http.response.error.UnauthorizedError;
5+
import dev.latvian.apps.tinyserver.http.response.error.client.UnauthorizedError;
66
import dev.latvian.apps.tinyserver.ws.WSHandler;
77

88
import java.io.IOException;
@@ -99,7 +99,7 @@ private static HTTPResponse stop(TestRequest req) {
9999
}
100100

101101
private static HTTPResponse error(TestRequest req) {
102-
throw new UnauthorizedError(null);
102+
throw new UnauthorizedError();
103103
}
104104

105105
private static HTTPResponse form(TestRequest req) {

0 commit comments

Comments
 (0)