gpt4 book ai didi

javascript - 如何响应 javascript 中的 SyntaxError

转载 作者:搜寻专家 更新时间:2023-11-01 04:19:24 27 4
gpt4 key购买 nike

我从 PHP 服务器获取数据,有时它会抛出警告。这些警告导致响应的解析抛出一个语法错误,该错误违背了我现有的所有 try/catch 代码并停止处理,使复杂对象处于无法恢复的部分状态。

我怎样才能捕捉到这些错误?我希望有机会让对象恢复到某种稳定状态。

理想情况下,我不会收到说明我应该重新考虑架构或更改 php 设置的答复。我想知道如何响应 JSON.parse() 抛出的语法错误。

谢谢,杰罗梅尔斯

编辑:

我注意到这个问题比我原先想象的要复杂。这是未捕获 SyntaxError 的代码:

generateSubmissionSuccessCallback: function (reloadOnSave) {
var self = this;

var submissionCallback = function(response) {
var processingError = false;

try
{
var responseObject = {};
if (self.isAspMode())
{
if (typeof response !== 'object') // Chrome auto-parses application/json responses, IE & FF don't
{
response = JSON.parse(response);
}

responseObject = {
entity: response.Payload,
success: response.Success,
message: response.Exception
};

if (jQuery.isArray(response.ValidationErrors))
{
responseObject.message += ' \r\n\r\nValidation Errors\r\n';
for (var i = 0, maxi = response.ValidationErrors.length; i < maxi; i++)
{
var error = response.ValidationErrors[i];
responseObject.message += error.Error + '\r\n';
}
}
}
else
{
responseObject = JSON.parse(response);
}

if (!responseObject || (responseObject.success !== undefined && responseObject.success !== true))
{
processingError = true;
var message = responseObject ? responseObject.message : response;
ErrorHandler.processError(
'An attempt to save failed with following message: \r\n' + message,
ErrorHandler.errorTypes.clientSide,
null,
jQuery.proxy(self.validatingAndSubmittingFinallyFunction, self));
}
else
{
// If this is a parent metaform, reload the entity, otherwise, close the metaform
if (self.metaformType === 'details')
{
if (self.substituteWhatToDoAfterSavingCallback)
{
self.substituteWhatToDoAfterSavingCallback(responseObject);
}
else if (reloadOnSave)
{
self.reloadCurrentEntity(true, responseObject.entity);
}

if (self.doesViewOutlineDefinePostSaveHook())
{
self.viewOutline.functions.postSaveHook(self);
}
}
else if (self.metaformType === 'childDetails')
{
// Reload the Grid by which this form was made
if (self.associatedGridId)
{
Metagrid.refresh(self.associatedGridId);
}

if (self.parentMetaform.associatedGridId && self.childPropertyName)
{
var annotation = self.parentMetaform.getAnnotationByPropertyName(self.childPropertyName);
if (annotation && annotation.hasPropertyOptions('updateParentMetaformAssociatedGrid'))
{
Metagrid.refresh(self.parentMetaform.associatedGridId, self.parentMetaform.entityId);
}
}

if (self.substituteWhatToDoAfterSavingCallback)
{
if (self.doesViewOutlineDefinePostSaveHook())
{
self.viewOutline.functions.postSaveHook(self);
}

self.substituteWhatToDoAfterSavingCallback(responseObject);
}
else
{
if (self.doesViewOutlineDefinePostSaveHook())
{
self.viewOutline.functions.postSaveHook(self);
}

self.disposeMetaform();
}
}
}
}
catch (ex)
{
processingError = true;
ErrorHandler.processError(
"Please immediately inform the authorities that: \r\n\r\n" + typeof response === 'string' ? response : JSON.parse(response) + "\r\n\r\nand:\r\n\r\n " + ex.message,
ErrorHandler.errorTypes.clientSide,
null,
jQuery.proxy(self.validatingAndSubmittingFinallyFunction, self));
}
finally
{
// If we are reporting an error to the user then we can't reset these state variables
// because in the case where this is a child form, the parent will close the form
// before the user has read the error.
if (!processingError)
{
self.validatingAndSubmittingFinallyFunction();
}
}
};

return jQuery.proxy(submissionCallback, self);
}

里面真的有很多东西,而且它适合很多结构。我不知道包含它是否真的有帮助。

最佳答案

假设您正在谈论 JSON 并且它引发错误(而不是向页面提供实际的 JavaScript):

var data;
try{
data = JSON.parse(jsonString);
}catch(e){
// handle the error here, if you like
}
if (typeof data !== "undefined"){
// Yay, we got some!
}

阅读更多关于 try...catch at MDN 的信息.

例如(来自 Chrome 的控制台):

> try{ JSON.parse('3/') }catch(e){ console.log('oh no!') }; console.log('OK!')
"oh no!"
"OK!"

关于javascript - 如何响应 javascript 中的 SyntaxError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10806447/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com