release- 调整异常处理业务逻辑

release-2024-08-08
pikaqiudeshujia 7 months ago
parent ef9e7f152a
commit 2a782b307b

@ -9,6 +9,7 @@ package constants;
* @Describe: 便
*/
public class ErrorConstants {
// 专辑部分
public final static String MUST_APPLY_FOR_REVIEW_IN_THE_NEW_STATE = "必须在新建状态下申请审核";
public final static String MUST_OPERATE_IN_PERSON = "必须本人操作";
@ -24,4 +25,6 @@ public class ErrorConstants {
public final static String PRICE_CAN_NOT_BE_EMPTY = "定价不能为空";
public final static String PRICING_MUST_BE_GREATER_THAN_0 = "定价必须大于0";
public final static String ALBUM_SONG_LIST_IS_EMPTY = "专辑歌曲为空";
}

@ -80,7 +80,7 @@ public class IdWorker {
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
throw new BizException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {

@ -12,6 +12,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
/**
@ -52,7 +53,7 @@ public class AlbumController {
@ApiOperation(value = "新增专辑", notes = "新增专辑")
@RequestMapping(value = "/add", method = RequestMethod.POST)
public Result<Void> add(@ApiParam(value = "Header中的token信息", required = true) @RequestHeader("Authorization") String token,
@ApiParam(value = "专辑信息", required = true) @RequestBody AlbumAddDTO addModel) {
@ApiParam(value = "专辑信息", required = true) @Validated @RequestBody AlbumAddDTO addModel) {
albumService.addAlbum(token, addModel);
return Result.success();
}

@ -4,29 +4,58 @@ import api.Result;
import api.StatusCode;
import exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.StringJoiner;
/**
*
*/
@Slf4j
@ControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result<Void> error(Exception e) {
log.error("执行出错", e);
return Result.failed(StatusCode.MUSIC_COMMON_FAILED);
}
@ExceptionHandler(value = BizException.class)
@ResponseBody
public Result<String> error(BizException e) {
log.info("业务错误:{}", e.getMessage());
StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum();
return Result.failed(statusCode, e.getMessage());
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result<Void> error(Exception e) {
log.error("执行出错", e);
return Result.failed(StatusCode.MUSIC_COMMON_FAILED);
}
@ExceptionHandler(value = BizException.class)
@ResponseBody
public Result<String> error(BizException e) {
log.info("业务错误:{}", e.getMessage());
StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.MUSIC_COMMON_FAILED : e.getCodeEnum();
return Result.failed(statusCode, e.getMessage());
}
/**
*
*
* @param e
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseBody
public Result<?> handle(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
StringJoiner joiner = new StringJoiner(";");
for (ObjectError error : bindingResult.getAllErrors()) {
String code = error.getCode();
String[] codes = error.getCodes();
String property = codes[1];
property = property.replace(code, "").replaceFirst(".", "");
String defaultMessage = error.getDefaultMessage();
joiner.add(property + defaultMessage);
}
return Result.failed(StatusCode.MUSIC_COMMON_FAILED, joiner.toString());
}
}

@ -1,8 +1,10 @@
package com.luoo.music.dto.response;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
@ -23,7 +25,7 @@ public class AlbumAddDTO implements Serializable {
*
*/
@ApiModelProperty("专辑名称")
@NotNull(message = "专辑名称不能为空")
@NotBlank(message = "专辑名称不能为空")
private String name;
/**
@ -43,7 +45,7 @@ public class AlbumAddDTO implements Serializable {
*
*/
@ApiModelProperty("专辑风格")
@NotNull(message = "专辑风格不能为空")
@NotBlank(message = "专辑风格不能为空")
private String mainStyle;
/**
@ -51,6 +53,7 @@ public class AlbumAddDTO implements Serializable {
*/
@ApiModelProperty("发行日期")
@NotNull(message = "发行日期不能为空")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date publishDate;
/**
@ -69,7 +72,7 @@ public class AlbumAddDTO implements Serializable {
*
*/
@ApiModelProperty("专辑封面")
@NotNull(message = "专辑封面不能为空")
@NotBlank(message = "专辑封面不能为空")
private String image;
/**

@ -20,7 +20,7 @@ import java.util.List;
public class AlbumSongAddDTO implements Serializable {
@ApiModelProperty("歌曲名称")
@NotNull(message = "歌曲名称不能为空")
@NotBlank(message = "歌曲名称不能为空")
private String name;
@ApiModelProperty("歌曲版本")
@ -39,22 +39,26 @@ public class AlbumSongAddDTO implements Serializable {
private String albumStr;
@ApiModelProperty("曲风")
@NotNull(message = "曲风不能为空")
private List<String> tags;
@ApiModelProperty("语种")
@NotNull(message = "语种不能为空")
private List<String> language;
@ApiModelProperty("作词")
@NotBlank(message = "作词不能为空")
private String lyricName;
@ApiModelProperty("作曲")
@NotBlank(message = "作曲不能为空")
private String compositionName;
@ApiModelProperty("歌词")
private String lyric;
@ApiModelProperty("歌曲url")
@NotNull(message = "歌曲不能为空")
@NotBlank(message = "歌曲不能为空")
private String url;
@ApiModelProperty("排序")
@ -64,5 +68,4 @@ public class AlbumSongAddDTO implements Serializable {
// @ApiModelProperty("mv")
// private String mvUrl;
}

@ -0,0 +1,72 @@
package com.luoo.music.dto.response;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
/**
* @Author: yawei.huang
* @Package: com.luoo.music.dto.response
* @Project: luoo_parent
* @Date: 2024/5/6 10:45
* @Filename: AlbumSongAddDTO
* @Describe:
*/
@Data
public class AlbumSongRequireDTO implements Serializable {
@ApiModelProperty("歌曲名称")
@NotBlank(message = "歌曲名称不能为空")
private String name;
@ApiModelProperty("歌曲版本")
@NotNull(message = "歌曲版本不能为空")
private Integer version;
@ApiModelProperty("是否收费")
@NotNull(message = "是否收费不能为空")
private Integer charge;
@ApiModelProperty("歌曲定价")
private Float price;
@ApiModelProperty("歌手")
@NotBlank(message = "歌手不能为空")
private String albumStr;
@ApiModelProperty("曲风")
@NotNull(message = "曲风不能为空")
private List<String> tags;
@ApiModelProperty("语种")
@NotNull(message = "语种不能为空")
private List<String> language;
@ApiModelProperty("作词")
@NotBlank(message = "作词不能为空")
private String lyricName;
@ApiModelProperty("作曲")
@NotBlank(message = "作曲不能为空")
private String compositionName;
@ApiModelProperty("歌词")
private String lyric;
@ApiModelProperty("歌曲url")
@NotBlank(message = "歌曲不能为空")
private String url;
@ApiModelProperty("排序")
@NotNull(message = "排序不能为空")
private Integer sort;
// @ApiModelProperty("mv")
// private String mvUrl;
}

@ -64,6 +64,7 @@ public class ArtistAlbum implements Serializable {
*
*/
@ApiModelProperty("发行日期")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date publishDate;
/**
@ -124,8 +125,8 @@ public class ArtistAlbum implements Serializable {
@ApiModelProperty("修改人")
private String updateUser;
@ApiModelProperty("收费类型 1-下载 2-试听及下载")
private Integer chargeType;
// @ApiModelProperty("收费类型 1-下载 2-试听及下载")
// private Integer chargeType;
@Transient
@ApiModelProperty("专辑歌曲绑定关系")

@ -2,10 +2,7 @@ package com.luoo.music.service;
import api.PageResult;
import com.luoo.music.dao.*;
import com.luoo.music.dto.response.AlbumAddDTO;
import com.luoo.music.dto.response.AlbumSearchDTO;
import com.luoo.music.dto.response.AlbumSongAddDTO;
import com.luoo.music.dto.response.AlbumUpdateDTO;
import com.luoo.music.dto.response.*;
import com.luoo.music.pojo.*;
import com.luoo.music.util.Constants;
import constants.ErrorConstants;
@ -13,6 +10,7 @@ import dto.UserLoginDto;
import enums.AlbumStateEnum;
import enums.SongInfoChargeEnum;
import enums.SongInfoStateEnum;
import exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
@ -24,6 +22,7 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import util.IdWorker;
import util.JwtUtil;
@ -31,7 +30,6 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.Valid;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
@ -178,11 +176,11 @@ public class AlbumService {
ArtistAlbum artistAlbum = artistAlbumDao.findById(id).get();
if (artistAlbum == null) {
// 专辑不存在,请刷新后重试
throw new RuntimeException(ErrorConstants.ALBUM_DOES_NOT_EXIST);
throw new BizException(ErrorConstants.ALBUM_DOES_NOT_EXIST);
}
if (ObjectUtils.equals(AlbumStateEnum.FORCE.getCode(), artistAlbum.getState())) {
// 专辑因不可抗力原因无法显示
throw new RuntimeException(ErrorConstants.CAUSE_OF_FORCE_MAJEURE);
throw new BizException(ErrorConstants.CAUSE_OF_FORCE_MAJEURE);
}
// 整个数据库所有的tag
@ -256,7 +254,7 @@ public class AlbumService {
artistAlbum.setCreateUser(user.getUserId());
} else {
// 用户校验失败,请重新登录
throw new RuntimeException(ErrorConstants.USER_VERIFICATION_FAILURE);
throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE);
}
artistAlbum.setState(AlbumStateEnum.SAVE.getCode());
artistAlbumDao.save(artistAlbum);
@ -285,12 +283,12 @@ public class AlbumService {
if (user != null) {
if (ObjectUtils.notEqual(user.getUserId(), artistAlbum.getCreateUser())) {
// 必须本人操作
throw new RuntimeException(ErrorConstants.USER_VERIFICATION_FAILURE);
throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE);
}
artistAlbum.setUpdateUser(user.getUserId());
} else {
// 用户校验失败,请重新登
throw new RuntimeException(ErrorConstants.USER_VERIFICATION_FAILURE);
throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE);
}
artistAlbumDao.save(artistAlbum);
@ -322,7 +320,7 @@ public class AlbumService {
ArtistAlbumSong artistAlbumSong = artistAlbumSongDao.findById(id).get();
if (artistAlbumSong.getId() == null) {
// 该专辑不存在此歌曲
throw new RuntimeException(ErrorConstants.THE_SONG_DOES_NOT_EXIST);
throw new BizException(ErrorConstants.THE_SONG_DOES_NOT_EXIST);
}
checkAlbum(token, artistAlbumSong.getAlbumId());
@ -344,11 +342,11 @@ public class AlbumService {
if (user != null) {
if (ObjectUtils.notEqual(user.getUserId(), artistAlbum.getCreateUser())) {
// 必须本人操作
throw new RuntimeException(ErrorConstants.MUST_OPERATE_IN_PERSON);
throw new BizException(ErrorConstants.MUST_OPERATE_IN_PERSON);
}
} else {
// 用户校验失败,请重新登录
throw new RuntimeException(ErrorConstants.USER_VERIFICATION_FAILURE);
throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE);
}
return artistAlbum;
}
@ -367,13 +365,49 @@ public class AlbumService {
addSongForAlbum(albumSongAddDTO, user, artistAlbum);
}
/**
*
*
* @param token token
* @param id id
*/
@Transactional(rollbackFor = Exception.class)
public void checkForApply(String token, String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(token);
ArtistAlbum artistAlbum = checkAlbum(token, id);
if (ObjectUtils.notEqual(AlbumStateEnum.SAVE.getCode(), artistAlbum.getState())) {
// 必须在新建状态下申请审核
throw new RuntimeException(ErrorConstants.MUST_APPLY_FOR_REVIEW_IN_THE_NEW_STATE);
throw new BizException(ErrorConstants.MUST_APPLY_FOR_REVIEW_IN_THE_NEW_STATE);
}
ArtistAlbum album = getOne(id);
List<SongInfo> songList = album.getSongList();
if(songList.isEmpty()) {
throw new BizException(ErrorConstants.ALBUM_SONG_LIST_IS_EMPTY);
}
for (SongInfo songInfo : songList) {
AlbumSongRequireDTO albumSongRequireDTO = new AlbumSongRequireDTO();
BeanUtils.copyProperties(songInfo, albumSongRequireDTO);
checkSong(albumSongRequireDTO);
}
artistAlbum.setState(AlbumStateEnum.IN_APPROVE.getCode());
artistAlbumDao.save(artistAlbum);
}
/**
*
*
* @param albumSongRequireDTO
*/
private void checkSong(@Validated AlbumSongRequireDTO albumSongRequireDTO) {
if(ObjectUtils.notEqual(SongInfoChargeEnum.CHARGE.getCode(), albumSongRequireDTO.getCharge())) {
if(albumSongRequireDTO.getPrice() == null) {
// 定价不能为空
throw new BizException(ErrorConstants.PRICE_CAN_NOT_BE_EMPTY);
}
if(albumSongRequireDTO.getPrice() <= 0) {
// 定价必须大于0
throw new BizException(ErrorConstants.PRICING_MUST_BE_GREATER_THAN_0);
}
}
}
@ -384,16 +418,16 @@ public class AlbumService {
* @param user
* @param artistAlbum
*/
private void addSongForAlbum(@Valid AlbumSongAddDTO albumSongAddDTO, UserLoginDto user, ArtistAlbum artistAlbum) {
private void addSongForAlbum(@Validated AlbumSongAddDTO albumSongAddDTO, UserLoginDto user, ArtistAlbum artistAlbum) {
if(ObjectUtils.notEqual(SongInfoChargeEnum.CHARGE.getCode(), artistAlbum.getChargeType())) {
if(ObjectUtils.notEqual(SongInfoChargeEnum.CHARGE.getCode(), albumSongAddDTO.getCharge())) {
if(albumSongAddDTO.getPrice() == null) {
// 定价不能为空
throw new RuntimeException(ErrorConstants.PRICE_CAN_NOT_BE_EMPTY);
throw new BizException(ErrorConstants.PRICE_CAN_NOT_BE_EMPTY);
}
if(albumSongAddDTO.getPrice() <= 0) {
// 定价必须大于0
throw new RuntimeException(ErrorConstants.PRICING_MUST_BE_GREATER_THAN_0);
throw new BizException(ErrorConstants.PRICING_MUST_BE_GREATER_THAN_0);
}
}
@ -410,7 +444,7 @@ public class AlbumService {
if (user != null) {
artistAlbum.setCreateUser(user.getUserId());
} else {
throw new RuntimeException(ErrorConstants.MUST_OPERATE_IN_PERSON);
throw new BizException(ErrorConstants.MUST_OPERATE_IN_PERSON);
}
songInfoDao.save(songInfo);

@ -4,10 +4,15 @@ import exception.BizException;
import api.Result;
import api.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.StringJoiner;
/**
*
*/
@ -29,4 +34,29 @@ public class BaseExceptionHandler {
StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.USER_COMMON_FAILED : e.getCodeEnum();
return Result.failed(statusCode, e.getMessage());
}
/**
*
*
* @param e
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ResponseBody
public Result<?> handle(MethodArgumentNotValidException e) {
BindingResult bindingResult = e.getBindingResult();
StringJoiner joiner = new StringJoiner(";");
for (ObjectError error : bindingResult.getAllErrors()) {
String code = error.getCode();
String[] codes = error.getCodes();
String property = codes[1];
property = property.replace(code, "").replaceFirst(".", "");
String defaultMessage = error.getDefaultMessage();
joiner.add(property + defaultMessage);
}
return Result.failed(StatusCode.MUSIC_COMMON_FAILED, joiner.toString());
}
}

@ -9,6 +9,7 @@ import constants.Constants;
import enums.UserProcessStatusEnum;
import enums.UserProcessTypeEnum;
import enums.UserStatusEnum;
import exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@ -66,7 +67,7 @@ public class ArtistService {
UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(artistRegisterDto.getUserName());
if(userInfoByUserName != null) {
throw new RuntimeException("该用户名已存在,请重新输入!");
throw new BizException("该用户名已存在,请重新输入!");
}
// 新增用户基本信息
@ -107,11 +108,11 @@ public class ArtistService {
// 审核记录
List<UserProcess> successList = userProcessDao.findUserProcessByUserIdAndTypeAndStatus(userInfo.getId(), UserProcessTypeEnum.ARTIST.getCode(), UserProcessStatusEnum.SUCCESS.getCode());
if(!successList.isEmpty()) {
throw new RuntimeException("该账号已审核通过,请勿重复申请");
throw new BizException("该账号已审核通过,请勿重复申请");
}
List<UserProcess> upApprovedList = userProcessDao.findUserProcessByUserIdAndTypeAndStatus(userInfo.getId(), UserProcessTypeEnum.ARTIST.getCode(), UserProcessStatusEnum.UNAPPROVED.getCode());
if(!upApprovedList.isEmpty()) {
throw new RuntimeException("该账号正在审核中,请勿重复申请");
throw new BizException("该账号正在审核中,请勿重复申请");
}
UserProcess userProcess = UserProcess.builder()
@ -133,7 +134,7 @@ public class ArtistService {
public void approve(UserProcessApproveDto userProcessApproveDto) {
if(ObjectUtils.equals(UserProcessStatusEnum.FAIL.getCode(), userProcessApproveDto.getStatus())
&& StringUtils.isBlank(userProcessApproveDto.getContent())) {
throw new RuntimeException("请填写拒绝理由!");
throw new BizException("请填写拒绝理由!");
}
UserProcess userProcessById = userProcessDao.findUserProcessById(userProcessApproveDto.getId());
@ -167,7 +168,7 @@ public class ArtistService {
artistUserDao.save(build);
} else {
throw new RuntimeException("请勿重复绑定!");
throw new BizException("请勿重复绑定!");
}
}
@ -180,7 +181,7 @@ public class ArtistService {
if(artistUser != null) {
artistUserDao.delete(artistUser);
} else {
throw new RuntimeException("没有绑定关系!");
throw new BizException("没有绑定关系!");
}
}

Loading…
Cancel
Save