Skip to content

Commit

Permalink
feat: ajout d'un widget d'erreur (#194)
Browse files Browse the repository at this point in the history
* feat: ajout d'un widget d'erreur
  • Loading branch information
K4ST0R authored Jun 7, 2023
1 parent cae94b1 commit 1822d47
Show file tree
Hide file tree
Showing 66 changed files with 2,697 additions and 1,245 deletions.
6 changes: 6 additions & 0 deletions server/src/http/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ ErrorNoDataForMillesime.prototype = Boom.notFound(`Pas de données pour le mill
export function ErrorCertificationNotFound() {}
ErrorCertificationNotFound.prototype = Boom.notFound("Certification inconnue");

export function ErrorCertificationsNotFound() {}
ErrorCertificationsNotFound.prototype = Boom.notFound("Certifications inconnues");

export function ErrorFormationNotFound() {}
ErrorFormationNotFound.prototype = Boom.notFound("Formation inconnue");

export function ErrorRegionaleNotFound() {}
ErrorRegionaleNotFound.prototype = Boom.notFound("Pas de données disponibles");

export function ErrorNoDataAvailable() {}
ErrorNoDataAvailable.prototype = Boom.notFound("Données non disponibles");
72 changes: 42 additions & 30 deletions server/src/http/routes/certificationsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import * as validators from "../utils/validators.js";
import { arrayOf, validate } from "../utils/validators.js";
import { checkApiKey } from "../middlewares/authMiddleware.js";
import { formatMillesime } from "../utils/formatters.js";
import { addCsvHeaders, addJsonHeaders, sendFilieresStats, sendStats, sendErrorSvg } from "../utils/responseUtils.js";
import {
addCsvHeaders,
addJsonHeaders,
sendFilieresStats,
sendStats,
sendImageOnError,
} from "../utils/responseUtils.js";
import { findCodeFormationDiplome } from "../../common/bcn.js";
import { getLastMillesimes, transformDisplayStat } from "../../common/stats.js";
import { getStatsAsColumns } from "../../common/utils/csvUtils.js";
Expand Down Expand Up @@ -65,7 +71,7 @@ export default () => {
router.get(
"/api/inserjeunes/certifications/:codes_certifications.:ext?",
tryCatch(async (req, res) => {
const { codes_certifications, millesime, vue, imageOnError, ...options } = await validate(
const { codes_certifications, millesime, vue, ...options } = await validate(
{ ...req.params, ...req.query },
{
codes_certifications: arrayOf(Joi.string().required()).default([]).min(1),
Expand All @@ -77,40 +83,46 @@ export default () => {

if (vue === "filieres" || codes_certifications.length > 1) {
const cfd = await findCodeFormationDiplome(codes_certifications);
const filieresStats = mapValues(
await CertificationsRepository.getFilieresStats({
code_formation_diplome: cfd,
millesime,
}),
transformDisplayStat()

const filieresStats = cfd
? mapValues(
await CertificationsRepository.getFilieresStats({
code_formation_diplome: cfd,
millesime,
}),
transformDisplayStat()
)
: {};

return sendImageOnError(
async () => await sendFilieresStats(filieresStats, res, options),
res,
{ type: "certifications" },
options
);
return sendFilieresStats(filieresStats, res, options);
}

try {
const code_certification = codes_certifications[0];
const exist = await CertificationsRepository.exist({ code_certification });
if (!exist) {
throw new ErrorCertificationNotFound();
}

const result = await CertificationsRepository.find({ code_certification, millesime });
if (!result) {
const millesimesAvailable = await CertificationsRepository.findMillesime({ code_certification });
throw new ErrorNoDataForMillesime(millesime, millesimesAvailable);
}
return sendImageOnError(
async () => {
const code_certification = codes_certifications[0];
const exist = await CertificationsRepository.exist({ code_certification });
if (!exist) {
throw new ErrorCertificationNotFound();
}

const stats = transformDisplayStat()(result);
return sendStats("certification", stats, res, options);
} catch (err) {
if (imageOnError) {
if (err instanceof ErrorCertificationNotFound || err instanceof ErrorNoDataForMillesime) {
return sendErrorSvg(res, options);
const result = await CertificationsRepository.find({ code_certification, millesime });
if (!result) {
const millesimesAvailable = await CertificationsRepository.findMillesime({ code_certification });
throw new ErrorNoDataForMillesime(millesime, millesimesAvailable);
}
}

throw err;
}
const stats = transformDisplayStat()(result);
return sendStats("certification", stats, res, options);
},
res,
{ type: "certifications" },
options
);
})
);

Expand Down
49 changes: 23 additions & 26 deletions server/src/http/routes/formationsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Joi from "joi";
import * as validators from "../utils/validators.js";
import { arrayOf, validate } from "../utils/validators.js";
import { checkApiKey } from "../middlewares/authMiddleware.js";
import { addCsvHeaders, addJsonHeaders, sendStats, sendErrorSvg } from "../utils/responseUtils.js";
import { addCsvHeaders, addJsonHeaders, sendStats, sendImageOnError } from "../utils/responseUtils.js";
import { formatMillesime } from "../utils/formatters.js";
import { compose, transformIntoCSV, transformIntoJSON } from "oleoduc";
import { getStatsAsColumns } from "../../common/utils/csvUtils.js";
Expand Down Expand Up @@ -76,7 +76,7 @@ export default () => {
router.get(
"/api/inserjeunes/formations/:uai-:code_certification.:ext?",
tryCatch(async (req, res) => {
const { uai, code_certification, millesime, direction, theme, ext, imageOnError } = await validate(
const { uai, code_certification, millesime, ...options } = await validate(
{ ...req.params, ...req.query },
{
uai: Joi.string()
Expand All @@ -88,34 +88,31 @@ export default () => {
}
);

try {
const exist = await FormationsRepository.exist({ uai, code_certification });
if (!exist) {
throw new ErrorFormationNotFound();
}

const result = await FormationsRepository.find({
uai,
code_certification,
millesime: formatMillesime(millesime),
});
return sendImageOnError(
async () => {
const exist = await FormationsRepository.exist({ uai, code_certification });
if (!exist) {
throw new ErrorFormationNotFound();
}

if (!result) {
const millesimesAvailable = await FormationsRepository.findMillesime({ uai, code_certification });
throw new ErrorNoDataForMillesime(millesime, millesimesAvailable);
}
const result = await FormationsRepository.find({
uai,
code_certification,
millesime: formatMillesime(millesime),
});

const stats = transformDisplayStat()(result);
return sendStats("formation", stats, res, { direction, theme, ext });
} catch (err) {
if (imageOnError) {
if (err instanceof ErrorFormationNotFound || err instanceof ErrorNoDataForMillesime) {
return sendErrorSvg(res, { direction, theme, ext });
if (!result) {
const millesimesAvailable = await FormationsRepository.findMillesime({ uai, code_certification });
throw new ErrorNoDataForMillesime(millesime, millesimesAvailable);
}
}

throw err;
}
const stats = transformDisplayStat()(result);
return sendStats("formation", stats, res, options);
},
res,
{ type: "formations" },
options
);
})
);

Expand Down
71 changes: 41 additions & 30 deletions server/src/http/routes/regionalesRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { tryCatch } from "../middlewares/tryCatchMiddleware.js";
import * as validators from "../utils/validators.js";
import { validate } from "../utils/validators.js";
import { checkApiKey } from "../middlewares/authMiddleware.js";
import { addCsvHeaders, addJsonHeaders, sendFilieresStats, sendStats, sendErrorSvg } from "../utils/responseUtils.js";
import {
addCsvHeaders,
addJsonHeaders,
sendFilieresStats,
sendStats,
sendImageOnError,
} from "../utils/responseUtils.js";
import { findCodeFormationDiplome } from "../../common/bcn.js";
import { getLastMillesimesFormations, transformDisplayStat } from "../../common/stats.js";
import { getStatsAsColumns } from "../../common/utils/csvUtils.js";
Expand Down Expand Up @@ -95,7 +101,7 @@ export default () => {
router.get(
"/api/inserjeunes/regionales/:region/certifications/:code_certification.:ext?",
tryCatch(async (req, res) => {
const { region, code_certification, millesime, vue, imageOnError, ...options } = await validate(
const { region, code_certification, millesime, vue, ...options } = await validate(
{ ...req.params, ...req.query },
{
...validators.region(),
Expand All @@ -108,40 +114,45 @@ export default () => {

if (vue === "filieres") {
const cfd = await findCodeFormationDiplome(code_certification);
const filieresStats = mapValues(
await RegionalesRepository.getFilieresStats({
code_formation_diplome: cfd,
millesime,
region,
}),
transformDisplayStat()
const filieresStats = cfd
? mapValues(
await RegionalesRepository.getFilieresStats({
code_formation_diplome: cfd,
millesime,
region,
}),
transformDisplayStat()
)
: {};

return sendImageOnError(
async () => await sendFilieresStats(filieresStats, res, options),
res,
{ type: "regionales", regionCode: region },
options
);
return sendFilieresStats(filieresStats, res, options);
}

try {
const exist = await RegionalesRepository.exist({ region, code_certification });
if (!exist) {
throw new ErrorRegionaleNotFound();
}

const result = await RegionalesRepository.find({ region, code_certification, millesime });
if (!result) {
const millesimesAvailable = await RegionalesRepository.findMillesime({ region, code_certification });
throw new ErrorNoDataForMillesime(millesime, millesimesAvailable);
}
return sendImageOnError(
async () => {
const exist = await RegionalesRepository.exist({ region, code_certification });
if (!exist) {
throw new ErrorRegionaleNotFound();
}

const stats = transformDisplayStat()(result);
return sendStats("certification", stats, res, options);
} catch (err) {
if (imageOnError) {
if (err instanceof ErrorRegionaleNotFound || err instanceof ErrorNoDataForMillesime) {
return sendErrorSvg(res, options);
const result = await RegionalesRepository.find({ region, code_certification, millesime });
if (!result) {
const millesimesAvailable = await RegionalesRepository.findMillesime({ region, code_certification });
throw new ErrorNoDataForMillesime(millesime, millesimesAvailable);
}
}

throw err;
}
const stats = transformDisplayStat()(result);
return sendStats("certification", stats, res, options);
},
res,
{ type: "regionales", regionCode: region },
options
);
})
);

Expand Down
10 changes: 7 additions & 3 deletions server/src/http/routes/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,16 @@ components:

imageOnError:
name: imageOnError
description: "Retourne une image en cas d'erreur 404"
description: |
Retourne une image en cas d'erreur 404
* true : retourne une image
* false : retourne le message d'erreur
* empty : retourne une image de type pixel vide
in: query
required: false
schema:
type: boolean
default: ""
type: string
enum: [ "true", "false", "empty"]

# Path parameters
uai:
Expand Down
Loading

0 comments on commit 1822d47

Please sign in to comment.