Improve error handling and ordering flows

This commit is contained in:
ecki
2026-04-06 17:50:00 +02:00
parent 8aa85d47a7
commit fef108cf53
8 changed files with 488 additions and 110 deletions
+47 -9
View File
@@ -2,6 +2,20 @@ const express = require("express")
const router = express.Router()
const db = require("../db")
const clean = (value) => value === "" ? null : value
const mapSubdomainError = (err, fallbackMessage) => {
let message = fallbackMessage
let status = 500
if(err.code === "ER_DUP_ENTRY"){
status = 400
message = "Subdomain existiert bereits"
}
return {status, message}
}
/* alle Subdomains laden */
router.get("/", async (req,res)=>{
@@ -24,7 +38,7 @@ res.json(rows)
}catch(e){
console.error("SUBDOMAIN LIST error:",e)
res.status(500).json({error:"DB error"})
res.status(500).json({error:"Subdomains konnten nicht geladen werden"})
}
@@ -46,7 +60,7 @@ res.json(rows)
}catch(e){
console.error("SUBDOMAIN DOMAIN error:",e)
res.status(500).json({error:"DB error"})
res.status(500).json({error:"Subdomains der Domain konnten nicht geladen werden"})
}
@@ -54,11 +68,19 @@ res.status(500).json({error:"DB error"})
router.delete("/:id", async (req,res)=>{
try{
await db.query("DELETE FROM subdomains WHERE id=?",[req.params.id])
res.json({success:true})
}catch(e){
console.error("DELETE subdomain error:",e)
res.status(500).json({error:"Subdomain konnte nicht geloescht werden"})
}
})
@@ -66,20 +88,31 @@ router.post("/", async (req,res)=>{
try{
const {domain_id, subdomain, ip_address} = req.body
const domainId = clean(req.body.domain_id)
const subdomain = req.body.subdomain ? String(req.body.subdomain).trim() : ""
const ipAddress = clean(req.body.ip_address)
if(!domainId){
return res.status(400).json({error:"Domain fehlt"})
}
if(!subdomain){
return res.status(400).json({error:"Subdomain darf nicht leer sein"})
}
await db.query(`
INSERT INTO subdomains
(domain_id, subdomain, ip_address)
VALUES (?,?,?)
`,[domain_id, subdomain, ip_address])
`,[domainId, subdomain, ipAddress])
res.json({success:true})
}catch(e){
console.error("CREATE subdomain error:",e)
res.status(500).json({error:"DB error"})
const {status, message} = mapSubdomainError(e, "Subdomain konnte nicht gespeichert werden")
res.status(status).json({error:message})
}
@@ -89,20 +122,26 @@ router.put("/:id", async (req,res)=>{
try{
const {subdomain, ip_address} = req.body
const subdomain = req.body.subdomain ? String(req.body.subdomain).trim() : ""
const ipAddress = clean(req.body.ip_address)
if(!subdomain){
return res.status(400).json({error:"Subdomain darf nicht leer sein"})
}
await db.query(`
UPDATE subdomains
SET subdomain=?, ip_address=?
WHERE id=?
`,[subdomain, ip_address, req.params.id])
`,[subdomain, ipAddress, req.params.id])
res.json({success:true})
}catch(e){
console.error("UPDATE subdomain error:",e)
res.status(500).json({error:"DB error"})
const {status, message} = mapSubdomainError(e, "Subdomain konnte nicht gespeichert werden")
res.status(status).json({error:message})
}
@@ -111,4 +150,3 @@ res.status(500).json({error:"DB error"})
module.exports = router