Skip to content

Commit

Permalink
Merge pull request #585 from shanwb/master
Browse files Browse the repository at this point in the history
add common result
  • Loading branch information
shanwb authored Aug 17, 2023
2 parents 4e9a58f + ee927fa commit 642b400
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
18 changes: 18 additions & 0 deletions jcommon/infra-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jcommon</artifactId>
<groupId>run.mone</groupId>
<version>1.4-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>infra-common</artifactId>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
31 changes: 31 additions & 0 deletions jcommon/infra-common/src/main/java/run/mone/common/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020 Xiaomi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package run.mone.common;

/**
* @author shanwb
* @date 2023-08-16
*/
public class ErrorCode {
public static final int SUCCESS = 0;

public static final int PARAM_ERROR = 400;
public static final int INTERNAL_ERROR = 500;

public ErrorCode() {
}
}
71 changes: 71 additions & 0 deletions jcommon/infra-common/src/main/java/run/mone/common/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020 Xiaomi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package run.mone.common;

import lombok.Data;

import java.io.Serializable;
import java.util.Map;

/**
* @author shanwb
* @date 2023-08-16
*/
@Data
public class Result<T> implements Serializable {
private int code;
private String message;
private T data;
private String traceId;
private Map<String, String> attributes;

public Result(int code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}

public Result(int code, String message, T data, String traceId) {
this.code = code;
this.message = message;
this.data = data;
this.traceId = traceId;
}

public static <T> Result<T> success(T data) {
return new Result(ErrorCode.SUCCESS, "success", data);
}

public static <T> Result<T> fail(int code, String message) {
return new Result(code, message, null);
}

public static <T> Result<T> fromException(Throwable e) {
return fail(ErrorCode.INTERNAL_ERROR, e.getMessage());
}

@Override
public String toString() {
return "Result{" +
"code=" + code +
", message='" + message + '\'' +
", data=" + data +
", traceId='" + traceId + '\'' +
", attributes=" + attributes +
'}';
}
}
1 change: 1 addition & 0 deletions jcommon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<module>openai</module>
<module>excel</module>
<module>match</module>
<module>infra-common</module>
</modules>


Expand Down

This file was deleted.

0 comments on commit 642b400

Please sign in to comment.