Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NO-ISSUE] hotfix: link between edge firewall and domain #2025

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/helpers/extract-api-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Extracts the first error message from an API response.
* For generic errors, returns the detail message.
* For field validation errors, returns "field: error message".
*
* @param {Object} httpResponse - The HTTP response object.
* @param {Object} httpResponse.body - The response body.
* @returns {string} The formatted error message.
*/
export const extractApiError = (httpResponse) => {
const errorBody = httpResponse.body

if (errorBody.detail) {
return errorBody.detail
}

const findFirstError = (obj) => {
for (const [key, value] of Object.entries(obj)) {
if (Array.isArray(value)) {
return `${key}: ${value[0]}`
}
if (typeof value === 'object' && value !== null) {
const nestedError = findFirstError(value)
if (nestedError) {
const cleanFieldName = nestedError.replace(/^[^:]+\./, '')
return cleanFieldName
}
}
}
return null
}

return findFirstError(errorBody) || 'Unknown error occurred'
}
5 changes: 5 additions & 0 deletions src/router/routes/domains-routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as DomainServices from '@/services/domains-services'
import * as DomainServicesV4 from '@/services/domains-services/v4'
import * as DigitalCertificatesServices from '@/services/digital-certificates-services'
import * as EdgeApplicationServices from '@/services/edge-application-services'
import * as EdgeFirewallServicesV4 from '@/services/edge-firewall-services/v4'

/** @type {import('vue-router').RouteRecordRaw} */
export const domainsRoutes = {
Expand Down Expand Up @@ -36,6 +37,8 @@ export const domainsRoutes = {
createDomainService: DomainServices.createDomainService,
listDigitalCertificatesService: DigitalCertificatesServices.listDigitalCertificatesService,
listEdgeApplicationsService: EdgeApplicationServices.listEdgeApplicationsService,
listEdgeFirewallService: EdgeFirewallServicesV4.listEdgeFirewallService,
loadEdgeFirewallService: EdgeFirewallServicesV4.loadEdgeFirewallService,
clipboardWrite: Helpers.clipboardWrite
},
meta: {
Expand All @@ -60,6 +63,8 @@ export const domainsRoutes = {
listDigitalCertificatesService: DigitalCertificatesServices.listDigitalCertificatesService,
listEdgeApplicationsService: EdgeApplicationServices.listEdgeApplicationsService,
loadDomainService: DomainServices.loadDomainService,
listEdgeFirewallService: EdgeFirewallServicesV4.listEdgeFirewallService,
loadEdgeFirewallService: EdgeFirewallServicesV4.loadEdgeFirewallService,
updatedRedirect: 'list-domains',
clipboardWrite: Helpers.clipboardWrite
},
Expand Down
3 changes: 2 additions & 1 deletion src/services/edge-firewall-services/v4/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { listEdgeFirewallService } from './list-edge-firewall-service'
import { cloneEdgeFirewallService } from './clone-edge-firewall-service'
import { loadEdgeFirewallService } from './load-edge-firewall-service'

/**
* @typedef {Object} ExportedServicesType - The type of the exported services
Expand All @@ -9,4 +10,4 @@ import { cloneEdgeFirewallService } from './clone-edge-firewall-service'
/**
* @type {ExportedServicesType}
*/
export { listEdgeFirewallService, cloneEdgeFirewallService }
export { listEdgeFirewallService, cloneEdgeFirewallService, loadEdgeFirewallService }
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { extractApiError } from '@/helpers/extract-api-error'
import { AxiosHttpClientAdapter, parseHttpResponse } from '@/services/axios/AxiosHttpClientAdapter'
import { makeEdgeFirewallBaseUrl } from './make-edge-firewall-base-url'

export const loadEdgeFirewallService = async ({ id }) => {
let httpResponse = await AxiosHttpClientAdapter.request({
url: `${makeEdgeFirewallBaseUrl()}/${id}`,
method: 'GET'
})

httpResponse = adapt(httpResponse)

return parseHttpResponse(httpResponse)
}

const adapt = ({ body, statusCode }) => {
if (statusCode !== 200) {
throw new Error(extractApiError({ body })).message
}

const payload = body.data
const parsedBody = {
id: payload.id,
name: payload.name,
isActive: payload.active,
edgeFunctionsEnabled: payload.modules.edge_functions_enabled,
networkProtectionEnabled: payload.modules.network_protection_enabled,
wafEnabled: payload.modules.waf_enabled,
debugRules: payload.debug_rules,
domains: payload.domains || [],
ddosProtectionUnmetered: true
}

return {
body: parsedBody,
statusCode
}
}
Loading
Loading