Web服务返回一个巨大的XML,我需要访问它的深层嵌套字段.例如:
return wsObject.getFoo().getBar().getBaz().getInt()
Run Code Online (Sandbox Code Playgroud)
问题是getFoo(),getBar(),getBaz()可能所有的回报null.
但是,如果我null在所有情况下检查,代码将变得非常冗长且难以阅读.此外,我可能会错过某些领域的支票.
if (wsObject.getFoo() == null) return -1;
if (wsObject.getFoo().getBar() == null) return -1;
// maybe also do something with wsObject.getFoo().getBar()
if (wsObject.getFoo().getBar().getBaz() == null) return -1;
return wsObject.getFoo().getBar().getBaz().getInt();
Run Code Online (Sandbox Code Playgroud)
写作是否可以接受
try {
return wsObject.getFoo().getBar().getBaz().getInt();
} catch (NullPointerException ignored) {
return -1;
}
Run Code Online (Sandbox Code Playgroud)
还是会被视为反模式?
java null exception nullpointerexception custom-error-handling
我正在尝试为我的Web应用程序设置自定义404错误页面.问题是该应用程序将部署到许多不同的环境中.有时它会在虚拟目录中,有时它不会.
我在名为ErrorPages的目录中有错误页面,并设置了我的配置,如下所示:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/VirtualDir/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
问题是当我将其部署到网站的根目录时,/VirtualDir需要删除该部分.如果我在部署之前删除它,那么我需要在部署到虚拟目录时重新添加它.有什么办法可以设置相对于虚拟目录而不是网站的路径?
我尝试过使用a ~,但这也不起作用,如下所示:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="~/ErrorPages/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud) 我有一个自定义异常类,如下所示:
<Serializable>
Public Class SamException
Inherits Exception
Public Sub New()
' Add other code for custom properties here.
End Sub
Public Property OfferBugSend As Boolean = True
Public Sub New(ByVal message As String)
MyBase.New(message)
' Add other code for custom properties here.
End Sub
Public Sub New(ByVal message As String, ByVal inner As Exception)
MyBase.New(message, inner)
' Add other code for custom properties here.
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
我在AJAX响应的返回中使用它来处理某些情况.
在我的AJAX响应的错误函数中,我确定错误属于我的自定义类型,如下所示:
....
ajax code....
.error = function (xhr, text, message) { …Run Code Online (Sandbox Code Playgroud) vb.net exception-handling exception custom-errors custom-error-handling
我正在尝试创建一些自定义错误页面,但似乎无法使500工作.
我有以下配置:
server {
listen 80;
root /var/www/devsite;
index index.php;
server_name devsite;
error_page 403 = /error.php?code=403;
error_page 404 = /error.php?code=404;
error_page 500 = /error.php?code=500;
location / {
try_files $uri =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)
起初,我认为这可能是因为它是一个PHP文件所以我改变了:
error_page 500 = /error.php?code=500;
Run Code Online (Sandbox Code Playgroud)
到静态页面:
error_page 500 /500.html
Run Code Online (Sandbox Code Playgroud)
但是当我打破一些PHP代码来触发它时,它仍然只显示一个带有500响应代码的空白页面.
然后我试着让它成为里面的最后一条规则,location ~ \.php$但同样的事情发生了.任何想法为什么自定义500页不起作用?
我还注意到,如果您尝试访问具有.php扩展名的"访问被拒绝"文件,它将不会显示自定义403页面并显示内置页面.有没有办法让规则覆盖.php文件呢?
我正致力于在.NET 中的HttpWebRequest/ HttpWebResponsetypes 之上构建一个流畅的REST客户端界面.到目前为止,这么好......但是我正在尝试开发一个可插拔的安全框架,它可以自动处理安全令牌协商,令牌刷新等.
由于HttpWebRequest/Response遇到400系列或500系列HTTP状态代码时的工作性质,我遇到了问题.他们不是简单地设置.StatusCode和.StatusDescription属性,而是允许你以任何你想要的方式处理它们WebException.一般来说,这可能不是问题...但是我们验证的方式(OAuth 2.0的派生),我需要处理某些400系列错误而不会发生异常.
有没有办法将HttpWebRequest/Response重新配置为NOT throw WebException,并允许消费者确定自己的错误处理?我知道有一些关于使用旧的Http1.0服务器处理Expect-100-Continue的方法......我很好奇是否有类似的循环方式来禁用WebExceptions.
(哦,只是无法抗拒......在RedGate上为了我非常好的朋友非法抢走反映器6的许可证免费版本......我可能能够解决这个问题如果我可以窥探代码,我自己......但是......反射器很遗憾地是一个不可行的选择,因为它已经通过自溶来消耗它.; P)
我正在学习使用WCF路由服务可以做些什么.仍然在'拧紧它,看看它能做什么'阶段.
我对路由服务的理解是,当消息通过时,服务将尝试将其传递给备份列表中首先出现的任何端点.如果失败了,它将继续尝试下一个,然后是下一个,直到任何一个有效或者没有什么可以尝试.
我想做的是访问该失败事件,以便我可以:
无法找到如何扩展WCF框架以获取此特定事件.
这是WCF路由服务可以做的事情吗?任何朝着正确方向的推动都将非常感激.
目前,我在IIS下托管了30-beh动态生成的路由服务(或者更准确地说,是Visual Studio 2010的ASP.NET开发服务器).我正在设置Global.asax中服务的路由,如下所示.
protected void Application_Start(object sender, EventArgs e)
{
List<Type> serviceTypes = ServiceUtility.GetServiceTypes();
foreach (Type st in serviceTypes)
{
string route = String.Format("Services/{0}.svc", ServiceUtility.GetServiceName(st));
RouteTable.Routes.Add(new ServiceRoute(route, new RoutingServiceHostFactory(st), typeof(System.ServiceModel.Routing.RoutingService)));
}
}
Run Code Online (Sandbox Code Playgroud)
ServiceUtility和RoutingServiceHostFactory是自定义类.请注意,IPolicyService是我感兴趣的程序集中的WCF服务契约接口.
public static class ServiceUtility
{
public static List<Type> GetServiceTypes()
{
Type policyInterfaceType = typeof(IPolicyService);
Assembly serviceContractsAssembly = Assembly.GetAssembly(policyInterfaceType);
Type[] serviceContractsAssemblyTypes = serviceContractsAssembly.GetTypes();
List<Type> serviceTypes = new List<Type>();
foreach (Type t in serviceContractsAssemblyTypes)
{
if (!t.IsInterface)
continue;
object[] attrib = …Run Code Online (Sandbox Code Playgroud) 通常,我必须使用多个库来处理不同的错误,或者定义自己的错误枚举.这使得编写可能必须处理来自不同源的错误的函数变得困难,然后返回其自己的错误代码.例如:
int do_foo_and_bar()
{
int err;
if ((err = libfoo_do_something()) < 0) {
// return err and indication that it was caused by foo
}
if ((err = libbar_do_something()) < 0) {
// return err and indication that it was caused by bar
}
// ...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想到了两种可能的解决方案:
int translate_foo_error(int err),我会为每个错误编写自己的字符串表示.struct my_error既包含标识库的枚举又包含错误代码的枚举.转换为字符串将被委托给每个库的适当函数.这似乎是一个经常出现的问题,所以我很好奇,这通常是如何处理的?看起来前者是大多数图书馆所做的事情,但后者的工作量较少,并且使用已经提供的工具.大多数教程只是向stderr打印一条消息并退出任何错误都没有用.我宁愿让每个函数指出出了什么问题,调用者可以从中决定如何处理它.
我为我的网站写了一个错误处理程序,如下所示:
function errorHandler($number, $string, $file, $line, $context, $type = '') {
// save stuff in DB
}
Run Code Online (Sandbox Code Playgroud)
我注册的是这样的:
set_error_handler('errorHandler', E_ALL);
Run Code Online (Sandbox Code Playgroud)
我保存了数据库中所有传递的变量,以及一个回溯来帮助我调试问题:
print_r(debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT), true)
Run Code Online (Sandbox Code Playgroud)
问题是我有时会收到此错误:
允许的内存大小为134217728字节耗尽(试图分配30084081字节)
错误处理程序在发出上述错误时运行的原因是我在创建Amazon S3对象(来自他们的PHP AWS库)后尝试使用未定义的变量.我假设,因为亚马逊AWS库非常庞大,以至于回溯正在吸引大量数据,导致内存不足错误(?).
我希望在可能的情况下包含一个回溯来帮助调试,但是如何防止调用该debug_backtrace()函数导致致命错误(在我的错误处理程序中,这有点讽刺......)?
我想为 Postgres CHECK IN 违规创建更具体的错误消息。例如,违反列上的以下 CHECK 约束:
management_zone varchar(15) NOT NULL CHECK (management_zone IN ('Marine', 'Terrestrial') ),
Run Code Online (Sandbox Code Playgroud)
应返回自定义错误消息,例如:“提示:检查拼写。仅允许的输入为:‘Marine’、‘Terrescial’。
到目前为止我见过的最好的解决方案是通过使用错误消息作为检查约束的名称来解决它,即
ADD CONSTRAINT c_managementzone_@Check_spelling._Only_allowed_inputs_are:_'Marine',_'Terrestrial' CHECK (management_zone IN ('Marine', 'Terrestrial') ),
Run Code Online (Sandbox Code Playgroud)
然后应用一个函数,通过用空格替换下划线来修复@符号后的错误消息,以使其更具可读性。然后使用 try 和 catch 语句调用该函数。
然而,这段代码是在我不熟悉的 t-sql 中,而且我也不了解足够的 PL/pgSQL 来转换和复制它。因此,我想知道是否有人可以建议如何在 postgres 中完成类似的事情,例如。使用函数和触发器?
t-sql 代码和解释可从此处获取,我将其复制粘贴到下面: https: //social.technet.microsoft.com/wiki/contents/articles/29187.t-sql-error-handling-for-check-约束.aspx
CREATE FUNCTION dbo.ufnGetClearErrorMessage2()
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE @Msg NVARCHAR(4000) = ERROR_MESSAGE() ;
DECLARE @ErrNum INT = ERROR_NUMBER() ;
DECLARE @ClearMessage NVARCHAR(4000) ;
IF @ErrNum = 547
BEGIN
/*--how to find @ClearMessage:
SELECT @msg ,
CHARINDEX('@', …Run Code Online (Sandbox Code Playgroud) postgresql triggers function check-constraints custom-error-handling
我试图涵盖类中所有其余的模板调用以进行异常处理。在 Spring Boot 应用程序中使用自定义异常处理和错误处理程序。
为此,我在配置中创建了一个休息模板 bean,并将其中的错误处理程序设置为我使用扩展 DefaultResponseErrorHandler 创建的自定义错误处理程序类。
public class BaseConfig {
@Bean
@Primary
RestTemplate restTemplate(@Autowired RestTemplateBuilder restTemplateBuilder) {
return restTemplateBuilder.errorHandler(new IPSRestErrorHandler()).build();
}
}
Run Code Online (Sandbox Code Playgroud)
@Component
public class IPSRestErrorHandler extends DefaultResponseErrorHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(IPSRestErrorHandler.class);
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (response.getStatusCode()
.series() == HttpStatus.Series.SERVER_ERROR) {
LOGGER.error("Server error with exception code : "+response.getStatusCode()+" with message : "+response.getStatusText());
throw ExceptionUtils.newRunTimeException("Server error with exception code : "+response.getStatusCode()+" with message : "+response.getStatusText());
} else if (response.getStatusCode()
.series() …Run Code Online (Sandbox Code Playgroud) exception ×2
php ×2
asp.net ×1
c ×1
c# ×1
function ×1
iis-7 ×1
java ×1
libraries ×1
nginx ×1
null ×1
postgresql ×1
resttemplate ×1
spring-boot ×1
triggers ×1
vb.net ×1
wcf ×1
wcf-routing ×1
webexception ×1