'use strict';

class FanfouError extends Error {
	constructor(error) {
		super();
		this.name = 'FanfouError';
		this.err = error;
		if (error.name === 'HTTPError') {
			const [contentType] = error.headers['content-type'].split(';');
			switch (contentType) {
				case 'application/json': {
					this.message = JSON.parse(error.body).error;
					break;
				}

				case 'text/html': {
					const titleMatch = error.body.match(/<title>(?<msg>.+)<\/title>/);
					if (titleMatch) {
						const {msg} = titleMatch.groups;
						this.message = msg;
					} else {
						this.message = `${error.statusCode} error`;
					}

					break;
				}

				case 'application/xml': {
					const errorMatch = error.body.match(/<error>(?<msg>.+)<\/error>/);
					if (errorMatch) {
						const {msg} = errorMatch.groups;
						this.message = msg;
					} else {
						this.message = `${error.statusCode} error`;
					}

					break;
				}

				default: {
					this.message = 'Unknown error';
					break;
				}
			}
		} else {
			this.message = error.message ? error.message : 'Unknown error';
		}
	}
}

module.exports = FanfouError;


@@ -191,16 +191,25 @@
 = error.
+response.
 headers%5B
@@ -326,24 +326,33 @@
 parse(error.
+response.
 body).error;
@@ -417,32 +417,41 @@
 leMatch = error.
+response.
 body.match(/%3Ctit
@@ -600,32 +600,41 @@
 sage = %60$%7Berror.
+response.
 statusCode%7D erro
@@ -724,16 +724,25 @@
 = error.
+response.
 body.mat
@@ -907,16 +907,25 @@
 $%7Berror.
+response.
 statusCo

Fix fanfou-error handler