insomnia/packages/insomnia-app/app/ui/components/modals/request-render-error-modal.js

96 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
import React, { PureComponent } from 'react';
import autobind from 'autobind-decorator';
import jq from 'jsonpath';
import RequestSettingsModal from '../modals/request-settings-modal';
import Modal from '../base/modal';
import ModalBody from '../base/modal-body';
import ModalHeader from '../base/modal-header';
2018-06-25 17:42:50 +00:00
import { showModal } from './index';
import Link from '../base/link';
@autobind
class RequestRenderErrorModal extends PureComponent {
2018-06-25 17:42:50 +00:00
constructor(props) {
super(props);
this.state = {
error: null,
};
}
2018-06-25 17:42:50 +00:00
_setModalRef(n) {
this.modal = n;
}
2018-06-25 17:42:50 +00:00
_handleShowRequestSettings() {
this.hide();
2018-06-25 17:42:50 +00:00
showModal(RequestSettingsModal, { request: this.state.request });
}
2018-06-25 17:42:50 +00:00
show({ request, error }) {
this.setState({ request, error });
this.modal.show();
}
2018-06-25 17:42:50 +00:00
hide() {
this.modal.hide();
}
2018-06-25 17:42:50 +00:00
renderModalBody(request, error) {
const fullPath = `Request.${error.path}`;
const result = jq.query(request, `$.${error.path}`);
2018-06-25 17:42:50 +00:00
const template = result && result.length ? result[0] : null;
const locationLabel =
2018-10-17 16:42:33 +00:00
template && template.includes('\n') ? `line ${error.location.line} of` : null;
return (
<div className="pad">
<div className="notice warning">
<p>
Failed to render <strong>{fullPath}</strong> prior to sending
</p>
<div className="pad-top-sm">
{error.path.match(/^body/) && (
2018-06-25 17:42:50 +00:00
<button
className="btn btn--clicky margin-right-sm"
onClick={this._handleShowRequestSettings}>
Adjust Render Settings
</button>
)}
2018-06-25 17:42:50 +00:00
<Link
button
href="https://support.insomnia.rest/article/40-template-tags"
className="btn btn--clicky">
Templating Documentation <i className="fa fa-external-link" />
</Link>
</div>
</div>
<p>
<strong>Render error</strong>
<code className="block selectable">{error.message}</code>
</p>
<p>
<strong>Caused by the following field</strong>
<code className="block">
{locationLabel} {fullPath}
</code>
</p>
</div>
2018-06-25 17:42:50 +00:00
);
}
2018-06-25 17:42:50 +00:00
render() {
const { request, error } = this.state;
return (
<Modal ref={this._setModalRef} freshState>
<ModalHeader>Failed to Render Request</ModalHeader>
2018-10-17 16:42:33 +00:00
<ModalBody>{request && error ? this.renderModalBody(request, error) : null}</ModalBody>
</Modal>
);
}
}
export default RequestRenderErrorModal;