Skip to content

Commit be630a3

Browse files
author
Montana Flynn
committed
Add error checking option
1 parent c813e02 commit be630a3

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

src/targets/go/native.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ var util = require('util');
44

55
module.exports = function (options) {
66

7+
// Let's Go!
8+
var code = [];
9+
10+
// Define Options
711
var opts = util._extend({
8-
errorChecking: false,
9-
timeout: -1,
12+
checkErrors: false,
1013
printBody: true
1114
}, options);
1215

@@ -22,16 +25,23 @@ module.exports = function (options) {
2225

2326
var bodyPresent = this.source.postData && this.source.postData.text;
2427

25-
// Let's Go!
26-
var code = [];
28+
var errorPlaceholder = opts.checkErrors ? "err" : "_"
29+
30+
var errorCheck = function() {
31+
if (opts.checkErrors) {
32+
code.push('\tif err != nil {');
33+
code.push('\t\tpanic(err)');
34+
code.push('\t}');
35+
}
36+
}
2737

2838
// Create boilerplate
2939
code.push('package main\n');
3040
code.push('import (');
3141
code.push('\t"fmt"');
3242
if (bodyPresent) code.push('\t"strings"');
3343
code.push('\t"net/http"');
34-
code.push('\t"io/ioutil"');
44+
if (opts.printBody) code.push('\t"io/ioutil"');
3545
code.push(')\n');
3646

3747
code.push('func main() {');
@@ -42,13 +52,13 @@ module.exports = function (options) {
4252

4353
// If we have body content or not create the var and reader or nil
4454
if (bodyPresent) {
45-
code.push('\tbody := ' + JSON.stringify(this.source.postData.text));
46-
req.body = 'strings.NewReader(body)';
55+
code.push('\tpayload := ' + JSON.stringify(this.source.postData.text));
56+
req.body = 'strings.NewReader(payload)';
4757
} else {
4858
req.body = 'nil';
4959
}
5060

51-
code.push('\treq, _ := http.NewRequest("' + req.method + '", url, ' + req.body + ')');
61+
code.push('\treq, ' + errorPlaceholder + ' := http.NewRequest("' + req.method + '", url, ' + req.body + ')');
5262

5363
// Add headers
5464
var headersPresent = this.source.headers && this.source.headers.length;
@@ -71,11 +81,12 @@ module.exports = function (options) {
7181
}
7282

7383
// Make request
74-
code.push('\tres, _ := client.Do(req)');
84+
code.push('\tres, ' + errorPlaceholder + ' := client.Do(req)');
7585

7686
// Get Body
77-
code.push('\tdefer res.Body.Close()');
78-
code.push('\tbody, _ := ioutil.ReadAll(res.Body)');
87+
if (opts.printBody) code.push('\tdefer res.Body.Close()');
88+
if (opts.printBody) code.push('\tbody, ' + errorPlaceholder + ' := ioutil.ReadAll(res.Body)');
89+
errorCheck()
7990

8091
// Print it
8192
code.push('\tfmt.Println(res)');

test/fixtures/output/go/native/application-form-encoded.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
body := "foo=bar&hello=world"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
13+
payload := "foo=bar&hello=world"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
1515
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

test/fixtures/output/go/native/application-json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
body := "{\"foo\": \"bar\"}"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
13+
payload := "{\"foo\": \"bar\"}"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
1515
req.Header.Add("Content-Type", "application/json")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

test/fixtures/output/go/native/full.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har?baz=abc&foo=bar&foo=baz"
13-
body := "foo=bar"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
13+
payload := "foo=bar"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
1515
req.Header.Add("Accept", "application/json")
1616
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1717
req.Header.Add("Cookie", "foo=bar; bar=baz")

0 commit comments

Comments
 (0)