Skip to content

Commit

Permalink
1.2.1 release
Browse files Browse the repository at this point in the history
commit 9d06828
Author: Stan Hutcheon <stanhutcheon@bigfork.co.uk>
Date:   Fri Apr 15 16:21:04 2016 +0100

    Improve sanity checks when no response from API

commit 6f4058c
Author: Stan Hutcheon <stanhutcheon@bigfork.co.uk>
Date:   Mon Nov 2 12:16:36 2015 +0000

    Fix tests, make error hander more generic

    - Update test accordingly
    - API error values are variable - the API docs have been updated recently.

commit 3e747fd
Author: Stan Hutcheon <stanhutcheon@bigfork.co.uk>
Date:   Thu Oct 29 16:24:06 2015 +0000

    Remove unsupported node versions from travis.yml
  • Loading branch information
Stan Hutcheon committed Apr 15, 2016
1 parent 5cf0062 commit 31d7183
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 38 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ node_js:
- "0.12"
- "0.11"
- "0.10"
- "0.8"
- "0.6"
- "iojs"
18 changes: 4 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function TinyPNG(opt, obj) {
var data,
info = {
url: false,
count: res.headers['compression-count'] || 0
count: (res && 'headers' in res && res.headers['compression-count']) || 0
};
if(err) {
err = new Error('Upload failed for ' + file.relative + ' with error: ' + err.message);
Expand All @@ -185,7 +185,7 @@ function TinyPNG(opt, obj) {
}

if(!err) {
if(data.error) err = this.handler(data); else if(data.output.url) {
if(data.error) err = this.handler(data, res.statusCode); else if(data.output.url) {
info.url = data.output.url;
} else err = new Error('Invalid TinyPNG response object returned for ' + file.relative);
}
Expand All @@ -207,18 +207,8 @@ function TinyPNG(opt, obj) {
});
},

handler: function(data) {
var errs = {
Unauthorized: 'The request was not authorized with a valid API key',
InputMissing: 'The file that was uploaded is empty or no data was posted',
BadSignature: 'The file was not recognized as a PNG or JPEG file. It may be corrupted or it is a different file type',
UnsupportedFile: 'The file was recognized as a PNG or JPEG file, but is not supported',
DecodeError: 'The file had a valid PNG or JPEG signature, but could not be decoded, most likely corrupt',
TooManyRequests: 'Your monthly upload limit has been exceeded',
InternalServerError: 'An internal error occurred during compression'
};

return new Error(data.error + ': ' + ((data.error in errs) ? errs[data.error] : data.message || 'unknown') + ' for ' + file.relative);
handler: function(data, status) {
return new Error((data.error || 'Unknown') + ' (' + status + '): ' + (data.message || 'No message returned') + ' for ' + file.relative);
},

get: function(cb) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-tinypng-compress",
"version": "1.2.0",
"version": "1.2.1",
"description": "TinyPNG API wrapper for compressing PNG & JPG images",
"main": "index.js",
"repository": "stnvh/gulp-tinypng-compress",
Expand Down
23 changes: 2 additions & 21 deletions test/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ var key = 'rlDAQuwa4AOtQPaekNpu-HgLOedHXOlh',
if(dry) console.info('dry flag set, skipping API tests...');

describe('tinypng', function() {
it('test has valid API key', function(done) {
var inst = new TinyPNG(key),
test = new TestFile();

test.contents = new Buffer('query', 'utf-8');

inst.request(test).upload(function(err, file) {
expect(err.message).to.match(/^BadSignature/);

done();
});
});

it('has correct bound object', function() {
var struct = ['conf', 'init', 'stream', 'request', 'hasher', 'utils', 'hash', 'stats'],
inst = new TinyPNG(key);
Expand Down Expand Up @@ -114,17 +101,11 @@ describe('tinypng', function() {
});

describe('#handler', function() {
it('returns correct error for API error value', function() {
var error = inst.request(new TestFile()).handler({error: 'Unauthorized'});

expect(error.message).to.equal('Unauthorized: The request was not authorized with a valid API key for image.png');
});

it('returns correct unknown error', function() {
var request = new inst.request(new TestFile());

expect(request.handler({ error: 'TestError'}).message).to.equal('TestError: unknown for image.png');
expect(request.handler({ error: 'TestError', message: 'test'}).message).to.equal('TestError: test for image.png');
expect(request.handler({ error: 'TestError'}, 500).message).to.equal('TestError (500): No message returned for image.png');
expect(request.handler({ error: 'TestError', message: 'test'}, 500).message).to.equal('TestError (500): test for image.png');
});
});

Expand Down

0 comments on commit 31d7183

Please sign in to comment.