Skip to content

Commit

Permalink
Improve api error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
musenkishi committed Jul 9, 2024
1 parent 357c25c commit e6f48f4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 42 deletions.
64 changes: 49 additions & 15 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import axios from "axios"
import TurndownService from "turndown"
import { NetworkResponse } from "../types/network"
import { OperationsResponse } from "../types/operation"
import { Network as MinimalNetwork, ProductsResponse } from "../types/product"
import { ApiResponse } from "../types/api"
import { NetworkData } from "../types/network"
import { OperationsData } from "../types/operation"
import { ProductNetwork, ProductsData } from "../types/product"
import { findToken, getSessionId } from "../util/credential"

const api = axios.create({
Expand Down Expand Up @@ -36,7 +37,7 @@ const getOperationsStatus = async (
url: string,
token: string,
sessionId: string,
callback: (operations: OperationsResponse) => void
callback: (operations: OperationsData) => void
) => {
try {
const response = await api.get(url, {
Expand All @@ -46,7 +47,12 @@ const getOperationsStatus = async (
"X-Requested-With": "XMLHttpRequest",
},
})
callback(response.data)
const data: ApiResponse<OperationsData> = response.data
if (data.status != "ok") {
console.error("Could not get outage data:", JSON.stringify(data))
} else {
callback(data.data)
}
} catch (err) {
console.error(err)
}
Expand All @@ -57,7 +63,7 @@ const getAvailableNetworks = async (
address: string,
token: string,
sessionId: string,
callback: (response: NetworkResponse) => void
callback: (response: NetworkData) => void
) => {
try {
const response = await api.post(
Expand All @@ -73,18 +79,41 @@ const getAvailableNetworks = async (
},
}
)
callback(response.data)

const data: ApiResponse<NetworkData> = response.data

if (data.status != "ok") {
console.error("Could not get outage data:", JSON.stringify(data))
} else {
switch (data.data.type) {
case "DEFAULT":
callback(data.data)
break

case "EMPTY_NETWORK":
case "COVERAGE_NOT_FOUND":
console.error(
"Could not find any networks available for address:",
address
)
break

default:
console.log("Unhandled network list type:", data.data.type)
break
}
}
} catch (err) {
console.error(err)
}
}

const getAvailableProducts = async (
url: string,
networks: MinimalNetwork[],
networks: ProductNetwork[],
token: string,
sessionId: string,
callback: (response: ProductsResponse) => void
callback: (response: ProductsData) => void
) => {
try {
const response = await api.post(
Expand All @@ -100,24 +129,29 @@ const getAvailableProducts = async (
},
}
)
callback(response.data)
const data: ApiResponse<ProductsData> = response.data
if (data.status != "ok") {
console.error("Could not get available products:", JSON.stringify(data))
} else {
callback(data.data)
}
} catch (err) {
console.error(err)
}
}

export const getProducts = (
address: string,
callback: (response: ProductsResponse) => void
callback: (response: ProductsData) => void
) => {
getTokens(tokenUrl, (csrfToken: string, cookieSession: string) => {
getAvailableNetworks(
apiNetworksUrl,
address,
csrfToken,
cookieSession,
(response: NetworkResponse) => {
const minimalNetworks: MinimalNetwork[] = response.data.networks.map(
(data: NetworkData) => {
const productNetworks: ProductNetwork[] = data.networks.map(
(network) => {
return {
city: network.city,
Expand All @@ -127,7 +161,7 @@ export const getProducts = (
)
getAvailableProducts(
apiProductsUrl,
minimalNetworks,
productNetworks,
csrfToken,
cookieSession,
(response) => {
Expand All @@ -141,7 +175,7 @@ export const getProducts = (

export const getOperations = (
postalCode: string,
callback: (operations: OperationsResponse) => void
callback: (operations: OperationsData) => void
) => {
const apiUrl = apiOperationsUrl + "/" + postalCode
getTokens(tokenUrl, (csrfToken: string, cookieSession: string) => {
Expand Down
8 changes: 4 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ const doPatrol = async (callback: (report: string) => void) => {
zonePromises.push(
new Promise<void>((resolve, reject) => {
getOperations(process.env.POSTAL_CODE, (operations) => {
const currentOutages = operations.data.open
const plannedOutages = operations.data.future
const currentOutages = operations.open
const plannedOutages = operations.future
if (currentOutages.length > 0 || plannedOutages.length > 0) {
const message = generateOutageMessage(
currentOutages,
Expand All @@ -68,9 +68,9 @@ const doPatrol = async (callback: (report: string) => void) => {
} else {
zonePromises.push(
new Promise<void>((resolve, reject) => {
getProducts(process.env.ADDRESS, async (result) => {
getProducts(process.env.ADDRESS, async (data) => {
const listedSubscription = getListedSubscription(
result.data.products,
data.products,
currentSubscription.speed
)

Expand Down
4 changes: 4 additions & 0 deletions src/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type ApiResponse<T> = {
status: string
data: T
}
11 changes: 4 additions & 7 deletions src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ export type Network = {
redirectUrl: string
}

export type NetworkResponse = {
status: string
data: {
type: string
networks: Network[]
}
}
export type NetworkData = {
type: string
networks: Network[]
}
13 changes: 5 additions & 8 deletions src/types/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ export type Operation = {
messages: Message[]
}

export type OperationsResponse = {
status: string
data: {
open: Operation[]
future: Operation[]
closed: Operation[]
all: Operation[]
}
export type OperationsData = {
open: Operation[]
future: Operation[]
closed: Operation[]
all: Operation[]
}
11 changes: 3 additions & 8 deletions src/types/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type Category = {
hidden: boolean
}

export type Network = {
export type ProductNetwork = {
city: string
network: string
}
Expand All @@ -44,7 +44,7 @@ export type Product = {
params: Param[]
category: Category
title: string
network: Network
network: ProductNetwork
id: number
internalTitle: string
description: string
Expand All @@ -55,12 +55,7 @@ export type Product = {
hidden: boolean
}

export type Data = {
export type ProductsData = {
products: Product[]
hasOtherProducts: boolean
}

export type ProductsResponse = {
status: string
data: Data
}

0 comments on commit e6f48f4

Please sign in to comment.