我有一个API,其中包含服务器引发错误时出错的有用描述(状态= 500)。该描述作为响应文本的一部分。我的客户端代码,使用Aurelia,使用通用方法通过function callRemoteService(apiName, timeout) {
return Promise.race([
this.http.fetch(apiName),
this.waitForServer(timeout || 5000) // throws after x ms
])
.then(response => response.json() )
.catch(err => {
if (err instanceof Response) {
// HERE'S THE PROBLEM.....
err.text().then(text => {
console.log('Error text from callRemoteService() error handler: ' + text);
throw new Error(text)
});
} else if (err instanceof Error) {
throw new Error(err.message);
} else {
throw new Error('Unknown error encountered from callRemoteService()');
}
});
}
调用api来进行调用:
throw
请注意,我想以一致的方式捕获服务器(获取或超时)错误,然后callRemoteService
只返回一个简单的错误消息到调用视图。我可以成功调用callRemoteService(this.apiName, this.apiTimeout)
.then(data => {
console.log('Successfully called \'' + this.apiName +
'\'! Result is:\n' + JSON.stringify(data, null, 2));
})
.catch(err => {
console.log('Error from \'' + this.apiName + '\':',err)
});
,在返回500时捕获错误:
fetch
但是,我在访问响应文本时遇到问题,因为text()
提供了返回承诺的Uncaught (in promise)
方法,而这干扰了我非常满意的承诺链接。上面的代码不起作用,只留下<?php
class Filemanager extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('upload');
$this->load->helper('text');
$this->load->library('pagination');
}
public function index($results = NULL) {
$data['title'] = 'File Manager';
$this_input_get_directory = $this->input->get('directory');
if (isset($this_input_get_directory)) {
$directory = scandir(FCPATH . 'images/catalog/' . $this_input_get_directory . '/', 1);
} else {
$directory = scandir(FCPATH . 'images/catalog/', 1);
}
$files = array_diff($directory, array('.', '..'));
$files_limit = 3;
$input_get_per_page = $this->uri->segment(2);
$input_get_per_page += $files_limit;
$config['base_url'] = base_url('filemanager');
$config['total_rows'] = count($files);
$config['per_page'] = $files_limit;
$config['uri_segment'] = 2;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$data['images'] = array();
foreach ($files as $file => $value) {
$url = '';
if (isset($this_input_get_directory)) {
$url .= $this_input_get_directory . '/';
} else {
$url .= '';
}
if ($file < $input_get_per_page && $file >= $input_get_per_page - $files_limit) {
if (is_dir(FCPATH . 'images/catalog/' . $value)) {
$data['images'][] = array(
'thumb' => '',
'type' => 'directory',
'href' => site_url('filemanager') . '?directory='. $url . $value,
'name' => $value
);
} elseif (is_file(FCPATH . 'images/catalog/' . $url . $value)) {
$data['images'][] = array(
'thumb' => $this->resize($value, 100, 100),
'type' => 'image',
'name' => $value
);
}
}
}
$this->load->view('template/common/filemanager_view', $data);
}
}
错误。
希望有一种访问该响应文本的好方法吗?
答案 0 :(得分:12)
这应该可以解决问题:
function callRemoteService(apiName, timeout = 5000) {
return Promise.race([
this.http.fetch(apiName)
.then(
r => r.json(),
r => r.text().then(text => throw new Error(text))
),
this.waitForServer(timeout)
]);
}
顺便说一句,我喜欢你用Promise.race
做的事情 - 很棒的技巧!