在Java中定义错误代码/字符串的最佳方式? [英] Best way to define error codes/strings in Java?

查看:104
本文介绍了在Java中定义错误代码/字符串的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个Web服务,而且我试图找出定义错误代码及其关联的错误字符串的最佳方式。。我需要一个数字错误代码和错误的字符串组合在一起。错误代码和错误字符串都将发送到访问Web服务的客户端。例如,当发生SQLException时,我可能想要执行以下操作:

  //示例:errorCode = 1,
// errorString =访问数据库时出现问题。
throw new SomeWebServiceException(errorCode,errorString);

客户端程序可能会显示消息:


发生错误#1:访问数据库有一个
问题。




我的第一个想法是使用错误代码的枚举,并覆盖 toString 方法来返回错误字符串。这是我想出的:

  public enum错误{
DATABASE {
@Override
public String toString(){
返回发生数据库错误。
}
},

DUPLICATE_USER {
@Override
public String toString(){
return此用户已存在。
}
},

//更多错误跟随
}

我的问题是:有没有更好的方法来做到这一点?我更喜欢代码中的解决方案,而不是从外部文件读取。我正在为这个项目使用Javadoc,并且可以在线记录错误代码,并让它们在文档中自动更新将是有帮助的。

解决方案

那么肯定会更好地实现枚举解决方案(这通常很不错):

  public enum错误{
DATABASE(0,发生数据库错误),
DUPLICATE_USER(1,此用户已存在);

private final int code;
private final String description;

private错误(int code,String description){
this.code = code;
this.description = description;
}

public String getDescription(){
return description;
}

public int getCode(){
return code;
}

@Override
public String toString(){
return code +:+ description;
}
}

您可能需要重写toString()才能返回描述反而不确定。无论如何,主要的一点是,您不需要单独重写每个错误代码。另外请注意,我已经明确指定了代码而不是使用序数值 - 这样可以更容易地更改顺序和添加/删除错误。



不要忘记这根本不是国际化的 - 除非您的Web服务客户端向您发送了一个区域描述,否则您无法轻易将其自身国际化。至少他们会在客户端使用i18n的错误代码...


I am writing a web service in Java, and I am trying to figure out the best way to define error codes and their associated error strings. I need to have a numerical error code and an error string grouped together. Both the error code and error string will be sent to the client accessing the web service. For example, when a SQLException occurs, I might want to do the following:

// Example: errorCode = 1, 
//          errorString = "There was a problem accessing the database."
throw new SomeWebServiceException(errorCode, errorString);

The client program might be shown the message:

"Error #1 has occured: There was a problem accessing the database."

My first thought was to used an Enum of the error codes and override the toString methods to return the error strings. Here is what I came up with:

public enum Errors {
  DATABASE {
    @Override
    public String toString() {
      return "A database error has occured.";
    }
  },

  DUPLICATE_USER {
    @Override
    public String toString() {
      return "This user already exists.";
    }
  },

  // more errors follow
}

My question is: Is there a better way to do this? I would prefer an solution in code, rather than reading from an external file. I am using Javadoc for this project, and being able to document the error codes in-line and have them automatically update in the documentation would be helpful.

解决方案

Well there's certainly a better implementation of the enum solution (which is generally quite nice):

public enum Error {
  DATABASE(0, "A database error has occured."),
  DUPLICATE_USER(1, "This user already exists.");

  private final int code;
  private final String description;

  private Error(int code, String description) {
    this.code = code;
    this.description = description;
  }

  public String getDescription() {
     return description;
  }

  public int getCode() {
     return code;
  }

  @Override
  public String toString() {
    return code + ": " + description;
  }
}

You may want to override toString() to just return the description instead - not sure. Anyway, the main point is that you don't need to override separately for each error code. Also note that I've explicitly specified the code instead of using the ordinal value - this makes it easier to change the order and add/remove errors later.

Don't forget that this isn't internationalised at all - but unless your web service client sends you a locale description, you can't easily internationalise it yourself anyway. At least they'll have the error code to use for i18n at the client side...

这篇关于在Java中定义错误代码/字符串的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆