DNS API error message

This commit is contained in:
2024-08-11 12:24:42 -04:00
parent 3e989992df
commit cb87f22d98
6 changed files with 100 additions and 108 deletions
+25
View File
@@ -0,0 +1,25 @@
'use strict';
let dnsErrors = {
unauthorized(instance){
let error = new Error('UnauthorizedDnsApi');
error.name = 'UnauthorizedDnsApi';
error.message = `Unauthorized call to ${instance.constructor ? instance.constructor.name : instance.name}`;
error.status = 424;
return error;
},
other(instance, status, message){
let error = new Error('OtherDnsApiError');
error.name = 'OtherDnsApiError';
error.message = `DNS API Error ${instance.constructor ? instance.constructor.name : instance.name}: `;
error.status = 424;
return error;
},
}
module.exports = {
dnsErrors
};
+18 -22
View File
@@ -1,7 +1,7 @@
'use strict';
const axios = require('axios');
const {dnsErrors} = require('./common');
class DigitalOcean{
static _keyMap = {
@@ -28,30 +28,34 @@ class DigitalOcean{
return name;
}
get axios(){
async axios(method, ...args){
try{
return axios.create({
let a = axios.create({
baseURL: 'https://api.digitalocean.com/v2/',
headers: {Authorization: `Bearer ${this.token}`}
});
return await a[method](...args);
}catch(error){
console.log(error.data);
if(!error.response) throw error;
if(error.response.data || error.response.data.id === 'Unauthorized'){
throw dnsErrors.unauthorized(this);
}
throw dnsErrors.other(this, error.response.status, error.response.data.message);
}
}
async listDomains(){
try{
let res = await this.axios.get('/domains');
for(let domain of res.data.domains){
domain.domain = domain.name
}
return res.data.domains;
}catch{}
let res = await this.axios('get', '/domains');
for(let domain of res.data.domains){
domain.domain = domain.name
}
return res.data.domains;
}
async getRecords(domain, options={}){
this.__typeCheck(options.type);
let res = await this.axios.get(`/domains/${domain}/records`, {params: options})
let res = await this.axios('get', `/domains/${domain}/records`, {params: options})
let records = [];
for(let record of res.data.domain_records){
@@ -70,12 +74,12 @@ class DigitalOcean{
if(!options.data) throw new Error(`${this.constructor.name} API: 'data' key is required for this action`)
if(options.name) options.name = this.__parseName(domain, options.name);
let res = await this.axios.post(`/domains/${domain}/records`, options);
let res = await this.axios('post', `/domains/${domain}/records`, options);
return res.data;
}
async deleteRecordById(domain, id){
let res = await this.axios.delete(`/domains/${domain}/records/${id}`);
let res = await this.axios('delete', `/domains/${domain}/records/${id}`);
}
async deleteRecords(domain, options){
@@ -100,14 +104,6 @@ if(require.main === module){(async function(){try{
// console.log('delete', await digi.deleteRecords('rm-rf.stream', {type: 'TXT'}))
// console.log('get', await digi.getRecords('rm-rf.stream', {type:'TXT', data:'890'}))
// let porkBun = new PorkBun(conf.porkBun.apiKey, conf.porkBun.secretApiKey);
// console.log(await porkBun.listDomains())
// console.log(await porkBun.deleteRecordById('holycore.quest', '415509355'))
// console.log('IIFE', await porkBun.createRecordForce('holycore.quest', {type:'A', name: 'testapi', content: '127.0.0.5'}))
// console.log('IIFE', await porkBun.getRecords('holycore.quest', {type:'A', name: 'testapi'}))
}catch(error){
console.log('IIFE Error:', error)
}})()}
+6 -1
View File
@@ -1,6 +1,7 @@
'use strict';
const axios = require('axios');
const {dnsErrors} = require('./common');
class PorkBun{
@@ -28,7 +29,11 @@ class PorkBun{
return res;
}catch(error){
throw new Error(`PorkPun API ${error.response.status}: ${error.response.data.message}`)
if(!error.response) throw error;
if(error.response.data.message.includes('Invalid API key')){
throw dnsErrors.unauthorized(this);
}
throw dnsErrors.other(this, error.response.status, error.response.data.message)
}
}