-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
236 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
group=io.github.ngyewch.twirp | ||
version=0.1.1 | ||
version=0.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
java/twirp-common/src/main/java/io/github/ngyewch/twirp/TwirpError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.github.ngyewch.twirp; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
public class TwirpError { | ||
private String code; | ||
private String msg; | ||
private Map<String, String> meta; | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getMsg() { | ||
return msg; | ||
} | ||
|
||
public void setMsg(String msg) { | ||
this.msg = msg; | ||
} | ||
|
||
public Map<String, String> getMeta() { | ||
return meta; | ||
} | ||
|
||
public void setMeta(Map<String, String> meta) { | ||
this.meta = meta; | ||
} | ||
|
||
public static TwirpError from(String code, String msg, Map<String, String> meta) { | ||
final TwirpError error = new TwirpError(); | ||
error.setCode(code); | ||
error.setMsg(msg); | ||
error.setMeta(meta); | ||
return error; | ||
} | ||
|
||
public static TwirpError from(TwirpErrorCode errorCode, String msg, Map<String, String> meta) { | ||
return from(errorCode.getCode(), msg, meta); | ||
} | ||
|
||
public static TwirpError fromJson(String s) throws IOException { | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
return objectMapper.readValue(s, TwirpError.class); | ||
} | ||
|
||
public static String toJson(TwirpError error) throws IOException { | ||
final ObjectMapper objectMapper = new ObjectMapper(); | ||
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
return objectMapper.writeValueAsString(error); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
java/twirp-common/src/main/java/io/github/ngyewch/twirp/TwirpErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package io.github.ngyewch.twirp; | ||
|
||
public enum TwirpErrorCode { | ||
CANCELED("canceled", 408), | ||
UNKNOWN("unknown", 500), | ||
INVALID_ARGUMENT("invalid_argument", 400), | ||
MALFORMED("malformed", 400), | ||
DEADLINE_EXCEEDED("deadline_exceeded", 408), | ||
NOT_FOUND("not_found", 404), | ||
BAD_ROUTE("bad_route", 404), | ||
ALREADY_EXISTS("already_exists", 409), | ||
PERMISSION_DENIED("permission_denied", 403), | ||
UNAUTHENTICATED("unauthenticated", 401), | ||
RESOURCE_EXHAUSTED("resource_exhausted", 429), | ||
FAILED_PRECONDITION("failed_precondition", 412), | ||
ABORTED("aborted", 409), | ||
OUT_OF_RANGE("out_of_range", 400), | ||
UNIMPLEMENTED("unimplemented", 501), | ||
INTERNAL("internal", 500), | ||
UNAVAILABLE("unavailable", 503), | ||
DATALOSS("dataloss", 500); | ||
|
||
private final String code; | ||
private final int httpStatus; | ||
|
||
TwirpErrorCode(String code, int httpStatus) { | ||
this.code = code; | ||
this.httpStatus = httpStatus; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public int getHttpStatus() { | ||
return httpStatus; | ||
} | ||
|
||
public static TwirpErrorCode fromCode(String code) { | ||
for (final TwirpErrorCode errorCode : TwirpErrorCode.values()) { | ||
if (errorCode.code.equals(code)) { | ||
return errorCode; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
java/twirp-common/src/main/java/io/github/ngyewch/twirp/TwirpException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.github.ngyewch.twirp; | ||
|
||
import java.util.Map; | ||
|
||
public class TwirpException extends RuntimeException { | ||
private final TwirpError error; | ||
|
||
public TwirpException(TwirpError error) { | ||
super(); | ||
|
||
this.error = error; | ||
} | ||
|
||
public TwirpException(String code, String msg, Map<String, String> meta) { | ||
this(TwirpError.from(code, msg, meta)); | ||
} | ||
|
||
public TwirpException(TwirpErrorCode errorCode, String msg, Map<String, String> meta) { | ||
this(TwirpError.from(errorCode, msg, meta)); | ||
} | ||
|
||
public TwirpError getError() { | ||
return error; | ||
} | ||
|
||
@Override | ||
public String getMessage() { | ||
final TwirpErrorCode errorCode = TwirpErrorCode.fromCode(error.getCode()); | ||
String s = ""; | ||
if (errorCode != null) { | ||
s += String.format("%s (%d): ", errorCode.getCode(), errorCode.getHttpStatus()); | ||
} else if (error.getCode() != null) { | ||
s += String.format("%s: ", error.getCode()); | ||
} | ||
if (error.getMsg() != null) { | ||
s += error.getMsg(); | ||
} | ||
return s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.