Skip to content

Commit

Permalink
fix: add message code
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Dec 18, 2023
1 parent 7cdf744 commit f032606
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

import static org.jboss.logging.Messages.getBundle;

@MessageBundle(projectCode = "CDI")
public interface CdiMessages {
@SuppressWarnings("checkstyle:MagicNumber")
@MessageBundle(projectCode = "DORA")
public interface Messages {
int BASE = 0;
@Inject
CdiMessages INSTANCE = getBundle(CdiMessages.class);
@Message(value = "代码错误: {0}", format = Message.Format.MESSAGE_FORMAT)
Messages INSTANCE = getBundle(Messages.class);

@Message(id = BASE + 1, value = "Code error: {0}", format = Message.Format.MESSAGE_FORMAT)
CodeException codeError(String message);
}
1 change: 1 addition & 0 deletions cdi/src/main/java/com/qwlabs/cdi/Primary.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.qwlabs.cdi;

import jakarta.inject.Qualifier;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.qwlabs.cdi.partition;

import com.qwlabs.cdi.CdiMessages;
import com.qwlabs.cdi.Messages;
import com.qwlabs.lang.BufferedCall;
import com.qwlabs.lang.C2;
import com.qwlabs.lang.F2;
Expand Down Expand Up @@ -59,7 +59,7 @@ private PartitionByParameter lookupPartitionBy(InvocationContext context) {
return buildPartitionByParameter(context, index);
}
}
throw CdiMessages.INSTANCE.codeError("Can not found @PartitionBy in method:" + context.getMethod().getName());
throw Messages.INSTANCE.codeError("Can not found @PartitionBy in method:" + context.getMethod().getName());
}

private boolean hasPartitionBy(Annotation[] parameterAnnotations) {
Expand Down Expand Up @@ -108,7 +108,7 @@ public <E, R> void add(BufferedCall<E, R> bufferedCall) {
bufferedCall.add((Collection<E>) data);
return;
}
throw CdiMessages.INSTANCE.codeError("PartitionBy can not support type " + type);
throw Messages.INSTANCE.codeError("PartitionBy can not support type " + type);
}

public <R, E> R call(List<E> data, InvocationContext context) throws Exception {
Expand All @@ -133,7 +133,7 @@ private <E> Object transformData(List<E> data) {
if (Collection.class.isAssignableFrom(type)) {
return data;
}
throw CdiMessages.INSTANCE.codeError("PartitionBy can not support type " + type.getName());
throw Messages.INSTANCE.codeError("PartitionBy can not support type " + type.getName());
}

public boolean needCall(int size) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.qwlabs.cdi.partition;

import com.qwlabs.cdi.CdiMessages;
import com.qwlabs.cdi.Messages;

import java.util.List;
import java.util.Set;
Expand All @@ -25,7 +25,7 @@ public static <R> R get(Stream<R> data, Class<R> returnType) {
if (Long.TYPE.isAssignableFrom(returnType) || Long.class.isAssignableFrom(returnType)) {
return (R) getLongData(data);
}
throw CdiMessages.INSTANCE.codeError("PartitionResult can not support type " + returnType.getName());
throw Messages.INSTANCE.codeError("PartitionResult can not support type " + returnType.getName());
}

private static <R> Long getLongData(Stream<R> data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ void should_exception() {

var exception = assertThrows(CodeException.class, () -> PartitionResult.get(data, Map.class));

assertThat(exception.getMessage(), is("代码错误: PartitionResult can not support type java.util.Map"));
assertThat(exception.getMessage(), is("DORA000001: Code error: PartitionResult can not support type java.util.Map"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public FeatureFlag get(String feature) {
return instances.stream()
.filter(instance -> Objects.equals(instance.feature(), feature))
.findFirst()
.orElseThrow(() -> FeatureFlagsMessages.INSTANCE.featureNotFound(feature));
.orElseThrow(() -> Messages.INSTANCE.featureNotFound(feature));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@


import com.qwlabs.exceptions.CodeException;
import jakarta.inject.Inject;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageBundle;

import jakarta.inject.Inject;

import static org.jboss.logging.Messages.getBundle;

@MessageBundle(projectCode = "FEATURE-FLAGS")
public interface FeatureFlagsMessages {
@SuppressWarnings("checkstyle:MagicNumber")
@MessageBundle(projectCode = "DORA")
public interface Messages {
int BASE = 3000;
@Inject
FeatureFlagsMessages INSTANCE = getBundle(FeatureFlagsMessages.class);
Messages INSTANCE = getBundle(Messages.class);

@Message(value = "Can not found feature: {0}", format = Message.Format.MESSAGE_FORMAT)
@Message(id = BASE, value = "Can not found feature: {0}", format = Message.Format.MESSAGE_FORMAT)
CodeException featureNotFound(String feature);
}
2 changes: 1 addition & 1 deletion lang/src/main/java/com/qwlabs/lang/LimitedRunner.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.qwlabs.lang;

import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;

import jakarta.validation.constraints.NotNull;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Consumer;

Expand Down
2 changes: 1 addition & 1 deletion lang/src/main/java/com/qwlabs/lang/QueueWorker.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.qwlabs.lang;

import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import jakarta.annotation.Nullable;

import java.time.Duration;
import java.util.Objects;
Expand Down
23 changes: 23 additions & 0 deletions message-ranges.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This document is intended to establish the ranges for message IDs in the various sub-projects.

# Range = owner
000001-000999 = cdi
001000-001999 = q-api
002000-002999 = storage-api
003000-003999 = feature-flags
004000-004999 =
005000-005999 =
006000-006999 =
007000-007999 =
008000-008999 =
009000-009999 =
010000-010999 =
011000-011999 =
012000-012999 =
013000-013999 =
014000-014999 =
015000-015999 =
016000-016999 =
017000-017999 =
018000-018999 =
019000-019999 =
27 changes: 27 additions & 0 deletions q-api/src/main/java/com/qwlabs/q/Messages.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.qwlabs.q;

import com.qwlabs.exceptions.BadRequestException;
import com.qwlabs.exceptions.CodeException;
import jakarta.inject.Inject;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageBundle;

import static org.jboss.logging.Messages.getBundle;


@SuppressWarnings("checkstyle:MagicNumber")
@MessageBundle(projectCode = "DORA")
public interface Messages {
int BASE = 1000;
@Inject
Messages INSTANCE = getBundle(Messages.class);

@Message(id = BASE, value = "Invalid dialect: {0}", format = Message.Format.MESSAGE_FORMAT)
CodeException invalidDialect(String dialect);

@Message(id = BASE + 1, value = "Invalid query: {0}", format = Message.Format.MESSAGE_FORMAT)
BadRequestException invalidQuery(String query);

@Message(id = BASE + 2, value = "Q Engine not found", format = Message.Format.MESSAGE_FORMAT)
CodeException engineNotFound();
}
25 changes: 0 additions & 25 deletions q-api/src/main/java/com/qwlabs/q/QMessages.java

This file was deleted.

4 changes: 2 additions & 2 deletions q-api/src/main/java/com/qwlabs/q/cdi/QEngineAware.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.qwlabs.q.cdi;

import com.qwlabs.cdi.SafeCDI;
import com.qwlabs.q.Messages;
import com.qwlabs.q.QEngine;
import com.qwlabs.q.QMessages;

import jakarta.enterprise.inject.Instance;

public interface QEngineAware {
default QEngine engine() {
return SafeCDI.select(QEngine.class)
.map(Instance::get)
.orElseThrow(QMessages.INSTANCE::engineNotFound);
.orElseThrow(Messages.INSTANCE::engineNotFound);
}
}
8 changes: 4 additions & 4 deletions q-api/src/main/java/com/qwlabs/q/cdi/QEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.qwlabs.cdi.dispatch.DispatchInstance;
import com.qwlabs.exceptions.ServiceException;
import com.qwlabs.lang.S2;
import com.qwlabs.q.Messages;
import com.qwlabs.q.QEngine;
import com.qwlabs.q.QMessages;
import com.qwlabs.q.conditions.QCondition;
import com.qwlabs.q.formatters.QFormatter;
import com.qwlabs.q.parsers.QParser;
Expand Down Expand Up @@ -34,7 +34,7 @@ public QEngineImpl(Instance<QFormatter> formatters,
@Nullable
public String format(@NotNull String dialect, @Nullable QCondition condition) {
if (S2.isEmpty(dialect)) {
throw QMessages.INSTANCE.invalidDialect(dialect);
throw Messages.INSTANCE.invalidDialect(dialect);
}
if (condition == null) {
return null;
Expand All @@ -50,7 +50,7 @@ public String format(@NotNull String dialect, @Nullable QCondition condition) {
@Nullable
public QCondition parse(@NotNull String dialect, @Nullable String query) {
if (S2.isEmpty(dialect)) {
throw QMessages.INSTANCE.invalidDialect(dialect);
throw Messages.INSTANCE.invalidDialect(dialect);
}
if (query == null) {
return null;
Expand All @@ -62,7 +62,7 @@ public QCondition parse(@NotNull String dialect, @Nullable String query) {
throw e;
} catch (Exception e) {
LOGGER.error("Invalid q. dialect:{} query: {}", dialect, query, e);
throw QMessages.INSTANCE.invalidQuery(query);
throw Messages.INSTANCE.invalidQuery(query);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.qwlabs.jackson.Jackson;
import com.qwlabs.storage.messages.StorageMessages;
import com.qwlabs.storage.messages.Messages;
import com.qwlabs.storage.services.StorageContext;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -45,8 +45,8 @@ private Map<String, Object> ofContext() {
}
if (context instanceof String) {
return Jackson.read((String) context, CONTENT_TYPE)
.orElseThrow(() -> StorageMessages.INSTANCE.invalidContent((String) context));
.orElseThrow(() -> Messages.INSTANCE.invalidContent((String) context));
}
throw StorageMessages.INSTANCE.invalidContent(context.toString());
throw Messages.INSTANCE.invalidContent(context.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.qwlabs.storage.minio;

import com.qwlabs.lang.S2;
import com.qwlabs.storage.messages.StorageMessages;
import com.qwlabs.storage.messages.Messages;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -16,13 +16,13 @@ public class MinioConfig {

public void validate() {
if (S2.isBlank(url)) {
throw StorageMessages.INSTANCE.lostConfig("url");
throw Messages.INSTANCE.lostConfig("url");
}
if (S2.isBlank(accessKey)) {
throw StorageMessages.INSTANCE.lostConfig("accessKey");
throw Messages.INSTANCE.lostConfig("accessKey");
}
if (S2.isBlank(secretKey)) {
throw StorageMessages.INSTANCE.lostConfig("secretKey");
throw Messages.INSTANCE.lostConfig("secretKey");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import com.google.common.collect.Lists;
import com.qwlabs.storage.messages.StorageMessages;
import com.qwlabs.storage.messages.Messages;
import com.qwlabs.storage.models.CompleteUploadCommand;
import com.qwlabs.storage.models.GetDownloadUrlCommand;
import com.qwlabs.storage.models.GetObjectCommand;
Expand Down Expand Up @@ -62,7 +62,7 @@ public String completeUpload(CompleteUploadCommand command) {
ListPartsResult result = minioClient.listParts(
command.getBucket(), command.getObjectName(), command.getUploadId());
if (result.partList().size() != command.getPartCount()) {
throw StorageMessages.INSTANCE.invalidPartCount(command.getPartCount(),
throw Messages.INSTANCE.invalidPartCount(command.getPartCount(),
result.partList().size());
}
List<Part> parts = result.partList();
Expand Down
8 changes: 4 additions & 4 deletions storage-s3/src/main/java/com/qwlabs/storage/s3/S3Config.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.qwlabs.storage.s3;

import com.qwlabs.lang.S2;
import com.qwlabs.storage.messages.StorageMessages;
import com.qwlabs.storage.messages.Messages;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -17,13 +17,13 @@ public class S3Config {

public void validate() {
if (S2.isBlank(url)) {
throw StorageMessages.INSTANCE.lostConfig("url");
throw Messages.INSTANCE.lostConfig("url");
}
if (S2.isBlank(accessKey)) {
throw StorageMessages.INSTANCE.lostConfig("accessKey");
throw Messages.INSTANCE.lostConfig("accessKey");
}
if (S2.isBlank(secretKey)) {
throw StorageMessages.INSTANCE.lostConfig("secretKey");
throw Messages.INSTANCE.lostConfig("secretKey");
}
}
}
Loading

0 comments on commit f032606

Please sign in to comment.