Skip to content

Commit

Permalink
Merge pull request #83 from metrico/jacovinus-fix-labels
Browse files Browse the repository at this point in the history
network alerts fix
  • Loading branch information
jacovinus authored Apr 21, 2022
2 parents 856606c + 8df4d03 commit 190bdf8
Show file tree
Hide file tree
Showing 40 changed files with 3,876 additions and 3,676 deletions.
110 changes: 69 additions & 41 deletions src/actions/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,69 @@
export const errorHandler = (url, error) => {
const { request, response } = error;

if (response?.statusText) {
const status = response?.status;
return {
message: "API " + response.statusText + ", Please adjust API URL",
status,
};
} else if (!url.includes(window.location.protocol)) {
return {
message: "Mixed Content Error, your View should be over same protocol as your API",
status: 500,
};
} else if (request) {
if(error.stack.includes('Network Error')) {
return {
message: "Invalid API URL, please adjust API URL",status:500
}
}
return {
message: "server time out",
status: response?.status,
};
} else if (error.stack.includes("Invalid URL")) {
return {
message: "Invalid API URL, please adjust API URL",
stauts: response?.status,
};
} else if (error?.response?.status === 404) {
return {
message: "Invalid API URL, please adjust API URL",
status: response?.status,
};
} else {
return {
message: "something went wrong with request",
status: response?.status,
};
}
};
export const errorHandler = (error) => {

const LABELS_URL = "/loki/api/v1/labels";
const QUERY_URL = "/loki/api/v1/query_range";

const { request, response } = error;
const url = error?.response?.request?.responseURL


let type = () => {
switch(url) {
case url?.includes(LABELS_URL):
return 'labels';
case url?.includes(QUERY_URL):
return 'query'
default: return 'labels'
}
}

if (response?.statusText) {
const status = response?.status;

return {
message: "API " + response.statusText + ", Please adjust API URL",
status,
type: type()
};
} else if (url && !url.includes(window.location.protocol)) {

return {
message: "Mixed Content Error, your View should be over same protocol as your API",
status: 500,
type : type()
};
} else if (request) {
if (error.stack.includes('Network Error')) {
return {
message: "Invalid API URL, please adjust API URL",
status: 500,
type: type()
}
}
return {
message: "server time out",
status: response?.status,
type: type()
};
} else if (error?.stack?.includes("Invalid URL")) {
return {
message: "Invalid API URL, please adjust API URL",
stauts: response?.status,
type: type()
};
} else if (error?.response?.status === 404) {
return {
message: "Invalid API URL, please adjust API URL",
status: response?.status,
type: type()
};
} else {
if (type === 'labels') return;

return {
message: "something went wrong with request",
status: response?.status,
type: type()
};
}
};
38 changes: 19 additions & 19 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export * from "./setStartTime";
export * from "./setStopTime";
export * from "./setQueryLimit";
export * from "./setQueryStep";
export * from "./setRangeOpen";
export * from "./setTimeRangeLabel";
export * from "./setApiUrl";
export * from "./setQuery";
export * from "./setIsSubmit";
export * from "./setMatrixData";
export * from "./setQueryHistory";
export * from "./setHistoryOpen";
export * from "./setApiError";
export * from "./errorHandler";
export * from "./setLabels";
export * from "./createAlert";
export * from "./removeAlert";
export * from "./setFromTime";
export * from "./setToTime";
export * from "./setStartTime";
export * from "./setStopTime";
export * from "./setQueryLimit";
export * from "./setQueryStep";
export * from "./setRangeOpen";
export * from "./setTimeRangeLabel";
export * from "./setApiUrl";
export * from "./setQuery";
export * from "./setIsSubmit";
export * from "./setMatrixData";
export * from "./setQueryHistory";
export * from "./setHistoryOpen";
export * from "./setApiError";
export * from "./errorHandler";
export * from "./setLabels";
export * from "./createAlert";
export * from "./removeAlert";
export * from "./setFromTime";
export * from "./setToTime";
138 changes: 69 additions & 69 deletions src/actions/loadLabelValues.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
import axios from "axios";
import { errorHandler } from "./errorHandler";
import { setApiError } from "./setApiError";
import { setLabels } from "./setLabels";
import setLabelValues from "./setLabelValues";
import setLoading from "./setLoading";


export default function loadLabelValues(label, labelList, apiUrl) {
if (!label || (label?.length <= 0 && label.lsList.length <= 0)) {
return () => {};
};

const url = apiUrl;

const origin = window.location.origin

const headers = {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Headers": ["Access-Control-Request-Headers", "Content-Type"],
"Content-Type": "application/json",
}

const options = {
method: "GET",
headers: headers,
mode: "cors",

};

return async (dispatch) => {
dispatch(setLoading(true))

await axios.get(`${url}/loki/api/v1/label/${label.name}/values`, options)
?.then(response => {
if (response?.data?.data) {
const values = response?.data?.data?.map?.((value) => ({
name: value,
selected: false,
inverted: false
}));

const lsList = [...labelList];
lsList.forEach((l) => {
if (l?.name === label?.name) {
l.values = [...values];
}
});
dispatch(setLabels(lsList))
} else if(!response) {
dispatch(setApiError('URL NOT FOUND'))
dispatch(setLabelValues([]))
}

dispatch(setLoading(false));
dispatch(setApiError(''))
dispatch(setLabelValues(response?.data?.data));

}).catch(error => {
dispatch(setLoading(false))
const { message } = errorHandler(url, error)
dispatch(setApiError(message || 'API NOT FOUND'))
dispatch(setLabelValues([]))
console.err(error)
})
}


}
import axios from "axios";
import { errorHandler } from "./errorHandler";
import { setApiError } from "./setApiError";
import { setLabels } from "./setLabels";
import setLabelValues from "./setLabelValues";
import setLoading from "./setLoading";


export default function loadLabelValues(label, labelList, apiUrl) {
if (!label || (label?.length <= 0 && label.lsList.length <= 0)) {
return () => {};
};

const url = apiUrl;

const origin = window.location.origin

const headers = {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Headers": ["Access-Control-Request-Headers", "Content-Type"],
"Content-Type": "application/json",
}

const options = {
method: "GET",
headers: headers,
mode: "cors",

};

return async (dispatch) => {
dispatch(setLoading(true))

await axios.get(`${url}/loki/api/v1/label/${label.name}/values`, options)
?.then(response => {
if (response?.data?.data) {
const values = response?.data?.data?.map?.((value) => ({
name: value,
selected: false,
inverted: false
}));

const lsList = [...labelList];
lsList.forEach((l) => {
if (l?.name === label?.name) {
l.values = [...values];
}
});
dispatch(setLabels(lsList))
} else if(!response) {
dispatch(setApiError('URL NOT FOUND'))
dispatch(setLabelValues([]))
}

dispatch(setLoading(false));
dispatch(setApiError(''))
dispatch(setLabelValues(response?.data?.data));

}).catch(error => {
dispatch(setLoading(false))
const { message } = errorHandler(url, error,'lavelValues')
dispatch(setApiError(message || 'API NOT FOUND'))
dispatch(setLabelValues([]))
console.error(error)
})
}


}
Loading

0 comments on commit 190bdf8

Please sign in to comment.